Solved

How to use the CreateProcessWithLogonW function

Posted on 2002-07-10
8
1,963 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
indy Tmemory stream non latin charcter issue 2 112
Making delphi communicate with a c# service 16 104
delphi parse string to params 3 123
Convert a string into a TDateTime 5 61
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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