Solved

How to use the CreateProcessWithLogonW function

Posted on 2002-07-10
8
1,884 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now