Solved

How to use the CreateProcessWithLogonW function

Posted on 2002-07-10
8
1,992 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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