LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CreateProcessWithLogonW ??

Hye,

I have to launch a program with administrator's rights in a simple user account on XP.
I've found the function CreateProcessWithLogonW() but it doesn't work. I've passed several hours on it but i don't know why it doesn't work.

By exemple, i've tried to launch cmd.exe:

CreateProcessWithLogonW(
(LPCWSTR)"account",
(LPCWSTR)"domain",
(LPCWSTR)"password",
(DWORD)"LOGON_WITH_PROFILE",
(LPCWSTR)"C:\\WINNT\\system32\\cmd.exe",
NULL,
(DWORD)"CREATE_UNICODE_ENVIRONMENT",
NULL,
NULL,
&lpStartupInfo,
&lpProcessInfo);

If someone can help me.
Thx,
Auma.
0 Kudos
Message 1 of 10
(7,622 Views)
Nobody ?
0 Kudos
Message 2 of 10
(7,606 Views)
Hello,

The function CreateProcessWithLogonW(...) is a function from the Microsoft Software Distribution Kit (SDK). First af all, be sure to have the SDK libraries installed with LabWindows/CVI. If the SDK libraries are not installed on your computer, you can install them from the LabWindows/CVI installer by selecting the proper component.

To have more information about the use of this function, I suggest you to take a look at the MSDN site. Here are some other web links.

- "CreateProcessWithLogonW":
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocesswithlogonw.asp

- "How to run a program as another user?" :
http://pluralsight.com/wiki/default.aspx/Keith.GuideBook/HowToRunAProgramAsAnotherUser.html

- "Managed CreateProcessWithLogonW":
http://geekswithblogs.net/khanna/archive/2005/02/09/22430.aspx

Regards,
0 Kudos
Message 3 of 10
(7,591 Views)
CVi does not support Unicode, so you probably don't want to use CREATE_UNICODE_ENVIRONMENT. Also, dont pass in "LOGON_WITH_PROFILE" with the quotes, they're getting passed in as strings insteads of the values they're supposed to represent. Same goes for the way you passed in CREATE_UNICODE_ENVIRONMENT.

And please make sure you check out all the remarks as documented for this function by the MSDN

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocesswithlogonw.asp

I hope this helps
Bilal Durrani
NI
0 Kudos
Message 4 of 10
(7,577 Views)
Thanks for your help.

Now, I've found the good syntax and the function works. But i have a problem with the "command line" parameter. It doesn't work. I've tried several methods but nothing.
The last thing i try, was to pass an argument when i launch the .exe, to recover it with "argv" and to translate it in the function with a "(LPWSTR) argv[1]" but it doesn't work.

But now it's the week-end, i'll look at that next week.
Thx,
Aurélien.
0 Kudos
Message 5 of 10
(7,568 Views)

Hi,

 

I am trying to run a command with CreateProcessWithLogonW, but I have an error while I link my project (undefined symbol  _CreateProcessWithLogonW)

 

Can you help to resolve my problem or send me an example ?

 

#include <windows.h>
#include <stdio.h>
#include <userenv.h>

#include <ansi_c.h>
#include <userint.h>
#include <toolbox.h>
#include "fenetres.h"
#include "spectrumclib.h"   
#include "commun.h"



int Test(void)
{
 STARTUPINFOW lpStartupInfo; 
 PROCESS_INFORMATION lpProcessInfo; 


 CreateProcessWithLogonW( 
       (LPCWSTR)"admin", 
       (LPCWSTR)"", 
       (LPCWSTR)"password", 
       (DWORD)LOGON_WITH_PROFILE, 
       (LPCWSTR)"cmd.exe /C", 
       NULL, 
       (DWORD)CREATE_WITH_USERPROFILE, 
       NULL, 
       NULL, 
       &lpStartupInfo, 
       &lpProcessInfo 
       ); return 0;
}

 

 

I thanks you

 

Sébastien

0 Kudos
Message 6 of 10
(6,682 Views)

Sébastien:

 

Do you have the Win32 API (Windows SDK) for CVI installed?

Did you add Advapi32.lib to your project?  (Using the menu in the CVI project window, goto Edit >> Add files to project >> Library (*.Lib), and browse to Advapi32.lib).  In older CVI versions, it was under c:\Program Files\National Instruments\MeasurementStudio\CVI\sdk\lib.  In LabWindows 2010, it's under c:\Program Files\National Instruments\CVI2010\sdk\lib\msvc.

0 Kudos
Message 7 of 10
(6,669 Views)

Hi,

 Yes I have Windows SDK for CVI installed.

The Advapi32.lib is in my project.

 

I send you my CVI 9 project.

 

I think I have a problem with include.

 

I thanks you

0 Kudos
Message 8 of 10
(6,658 Views)

Sebastien:

 

Your includes look OK.  CreateProcessWithLogonW() is defined in winbase.h, which is included in windows.h.

 

However, CreateProcessWithLogonW() is not defined for all versions of Windows.  If you search for CreateProcessWithLogonW in winbase.h, you'll see that it is preceeded with the statement

 

#if(_WIN32_WINNT >= 0x0500)

 

So you need to #define _WIN32_WINNT

 

The definitions are here:

http://msdn.microsoft.com/en-us/library/aa383745.aspx

 

XP is 0x0501

Vista is 0x600

Windows 7 is 0x601  (Interesting.  Not 0x0700?)

 

So if you're running Vista, insert the following statement BEFORE #include <windows.h>

 

#define _WIN32_WINNT 0x600

#include <windows.h>

0 Kudos
Message 9 of 10
(6,647 Views)

P.S.

 

You should also check the return value for CreateProcessWithLogonW.  It's a boolean: 0 means it failed, non-zero means it succeeded.  For example

 

if (!CreateProcessWithLogonW(...))

// report error

 

Take a look at the MSDN help for CreateProcessWithLogonW

0 Kudos
Message 10 of 10
(6,643 Views)