Solved

How to use the CreateProcessWithLogonW function

Posted on 2002-07-10
8
2,101 Views
Last Modified: 2010-04-04
Hello guys! Here is all my code
Why the execution of the CreateProcessWithLogonW may not work?



program Launch;
{$APPTYPE CONSOLE}
uses
  SysUtils, Windows,
  Dialogs;


function CreateProcessWithLogonW(lpUsdeerName,lpDomain,lpPassword: LPCWSTR;
  dwLogonFlags:dword; lpApplicationName : LPCWSTR; lpCommandLine: LPWSTR; dwCreateionFlags:dword;
  lpEnvironment:Pointer; lpCurrentDirectory: LPCWSTR; lpstartupinfo:tstartupinfo;
  lpProcessInfo:TProcessInformation): BOOL; external 'C:\WINNT\system32\advapi32.dll';



const
  pProgramName = 'ClipBoarder.exe';
  pUserName    = 'Administrator';
  pUserDomain  = '';
  pUserPassword = 'pwd';
  pNullStr = '';

var
  StartInfo  : TStartupInfo;
  ProcInfo   : TProcessInformation;
  CreateOK   : Boolean;
  Count : Integer;

  ProgramName,
  UserName,
  UserDomain,
  UserPassword,
  NullStr : PWideChar;

begin
  ProgramName := StringToOleStr(pProgramName);
  UserName := StringToOleStr(pUserName);
  UserDomain := StringToOleStr(pUserDomain);
  UserPassword := StringToOleStr(pUserPassword);
  NullStr := StringToOleStr(pNullStr);


  FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,SizeOf(TProcessInformation),#0);

  StartInfo.cb := SizeOf(TStartupInfo);
  StartInfo.dwFlags := startf_UseShowWindow;
  StartInfo.wShowWindow := 4;

 try
{   CreateOK := CreateProcess(pProgramName,nil, nil, nil,False,
                 CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
                 nil, nil, StartInfo, ProcInfo);
}
   CreateOK := CreateProcessWithLogonW(
              UserName,
              UserDomain,
              UserPassword,
              0,
              ProgramName,
              nil,
              CREATE_DEFAULT_ERROR_MODE or NORMAL_PRIORITY_CLASS,
              nil,
              nil,
              startinfo,
              procinfo);
   if not CreateOK then RaiseLastWin32Error;

  except
    on E: Exception do
    begin
      writeln(E.Message );
    end;
  end
end.
0
Comment
Question by:penich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 7143360
Are you sure that you gave in the correct password and user name? And what is the exact error message, please?

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7143375
Have you tried giving in the full program path? Also better give it in as the command line parameter, leave the program name to nil. That works better according to my experience.

Is that domain information correct? An empty strings is quite strange. That does NOT qualify for the local machine, as far as I know!
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 7143557
I think that the current user account also needs privileges to impersonate other accounts.
Anyway, exact error code/message might be useful. <g>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:penich
ID: 7144121
Additional info

1. Login and Password are correct
2. I tried to pass the full programname parameter like:
'c:\temp\clipboarder.exe'
'c:\\temp\\clipboarder.exe'
'clipboarder.exe'
3. My PC name is 'windev-3' but i don't have a domain name at all - very simple office network
4. There was no any error message at all - I could not even launch GetLastError function because the Access Violation occurs derectly in the CreateProcessWithLogonW function. (something like 'Access Violation ... in ntdll ....' )
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7144471
What O/S and service pack are you using? According to M$' knowledgebase, this API function can only be used in W2K and higher.


Good luck!!
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7144513
What O/S and service pack are you using? According to M$' knowledgebase, this API function can only be used in W2K and higher.


Good luck!!
0
 

Author Comment

by:penich
ID: 7145018
....
win2k, SP2
0
 
LVL 20

Accepted Solution

by:
Madshi earned 500 total points
ID: 7145330
You didn't say that you get an exception. Well, I checked it out. There were several errors in how you defined CreateProcessWithLogonW. You forgot stdcall, also startupInfo and processInformation needed "const" and "var". This one works on my PC:

function CreateProcessWithLogonW(lpUsdeerName,lpDomain,lpPassword: LPCWSTR;
  dwLogonFlags:dword; lpApplicationName : LPCWSTR; lpCommandLine: LPWSTR; dwCreateionFlags:dword;
  lpEnvironment:Pointer; lpCurrentDirectory: LPCWSTR; const lpstartupinfo:tstartupinfo;
  var lpProcessInfo:TProcessInformation): BOOL; stdcall; external 'advapi32.dll';

function CreateProcessWithLogon(user, domain, password, cmdLine: string; var pi: TProcessInformation) : dword;
var si : TStartupInfo;
begin
  ZeroMemory(@si, sizeOf(si));
  si.cb := sizeOf(si);
  ZeroMemory(@pi, sizeOf(pi));
  result := CreateProcessWithLogonW(PWideChar(wideString(user)),
                                    PWideChar(wideString(domain)),
                                    PWideChar(wideString(password)),
                                    0, nil,
                                    PWideChar(wideString(cmdLine)),
                                    CREATE_DEFAULT_ERROR_MODE or NORMAL_PRIORITY_CLASS,
                                    nil, nil, si, pi);
  if not result then
       result := GetLastError
  else result := 0;
end;

Regards, Madshi.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question