Jump to content
kvk1989

function error

Recommended Posts

hi , i'm trying to install driver function but give me error

here is my code 

Function InstalUinstallMTP_Driver(IsInstall: Boolean = true): Boolean;
var
  driverpath, dir, cmd: string;
begin
  result := false;
  driverpath := ExtractFilepath(application.ExeName) + 'adb\mtp\drivers\SAMSUNG_Android.inf';
  if not fileexists(driverpath) then
  begin
    addlog('Driver not exist, ');
    exit;
  end;
  dir := ExtractFilepath(application.ExeName) + '\drivers';

  if IsWindows64 then
    driverpath := 'install_x64.exe'
  else
    driverpath := 'install_x86.exe';

  if IsInstall then
  begin
    cmd := driverpath + ' install "--inf=SAMSUNG_Android.inf"';
    cmd := Shell(cmd, dir);  //// here error 
    if cmd.Contains('starting devices') then
      result := true;
  end
  else
  begin
    cmd := driverpath + ' uninstall "--inf=SAMSUNG_Android.inf"';
    cmd := Shell(cmd, dir);
  //  addlog(cmd);
    if cmd.Contains('starting devices') then
      result := true;
  end;

 

Share this post


Link to post

What is the exact error you get?

Do you get the error at compile time or at runtime?

What is the function Shell? I guess it is similar to ShellExecute?

Share this post


Link to post
10 minutes ago, FPiette said:

What is the exact error you get?

Do you get the error at compile time or at runtime?

What is the function Shell? I guess it is similar to ShellExecute?

yes , runtime 

here shell codes 

function IsWinNT: Boolean;
var
  OSV: OSVERSIONINFO;
begin
  OSV.dwOSVersionInfoSize := sizeof(OSV);
  GetVersionEx(OSV);
  Result := OSV.dwPlatformId = VER_PLATFORM_WIN32_NT;
end;

function Shell(Cmd: string; dirpath: string): string;
var
  Buffer: array [0 .. 10096] of char;
  si: STARTUPINFO;
  sa: SECURITY_ATTRIBUTES;
  sd: SECURITY_DESCRIPTOR;
  pi: PROCESS_INFORMATION;
  newstdin, newstdout, read_stdout, write_stdin: THANDLE;
  exitcod, bread, avail: cardinal;
  Str: ansistring;
begin

  Result := '';
  SetCurrentDir(dirpath); // ExtractFilePath(Application.ExeName));
  if IsWinNT then
  begin
    InitializeSecurityDescriptor(@sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(@sd, True, nil, False);
    sa.lpSecurityDescriptor := @sd;
  end
  else
    sa.lpSecurityDescriptor := nil;
  sa.nLength := sizeof(SECURITY_ATTRIBUTES);
  sa.bInheritHandle := True;
  if CreatePipe(newstdin, write_stdin, @sa, 0) then
  begin
    if CreatePipe(read_stdout, newstdout, @sa, 0) then
    begin
      GetStartupInfo(si);
      with si do
      begin
        dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
        wShowWindow := SW_HIDE;
        hStdOutput := newstdout;
        hStdError := newstdout;
        hStdInput := newstdin;
      end;
      FillChar(Buffer, sizeof(Buffer), 0);
      GetEnvironmentVariable('COMSPEC', @Buffer, sizeof(Buffer) - 1);
      StrCat(@Buffer, PChar(' /c ' + Cmd));
      if CreateProcess(nil, @Buffer, nil, nil, True, CREATE_NEW_CONSOLE, nil,
        nil, si, pi) then
      begin
        Str := #13;
        WriteFile(write_stdin, PansiChar(Str)^, Length(Str), bread, nil);
        repeat
          application.ProcessMessages;
          PeekNamedPipe(read_stdout, @Buffer, sizeof(Buffer) - 1, @bread,
            @avail, nil);
          if bread > 0 then
          begin
            FillChar(Buffer, sizeof(Buffer), 0);
            ReadFile(read_stdout, Buffer, bread, bread, nil);
            Result := Result + ansistring(PansiChar(@Buffer));
          end;
          GetExitCodeProcess(pi.hProcess, exitcod);
        until (exitcod <> STILL_ACTIVE) and (bread = 0);
      end;
      CloseHandle(read_stdout);
      CloseHandle(newstdout);
    end;
    CloseHandle(newstdin);
    CloseHandle(write_stdin);
  end;
end;

function Cmd(Cmd: string): string;
var
  Buffer: array [0 .. 10096] of PChar;
  si: STARTUPINFO;
  sa: SECURITY_ATTRIBUTES;
  sd: SECURITY_DESCRIPTOR;
  pi: PROCESS_INFORMATION;
  newstdin, newstdout, read_stdout, write_stdin: THANDLE;
  exitcod, bread, avail: cardinal;
  Str: string;
  StopExecution: BOOL;
  path: string;
begin

  Result := '';
  SetCurrentDir(ExtractFilePath(application.ExeName));
  if IsWinNT then
  begin
    InitializeSecurityDescriptor(@sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(@sd, True, nil, False);
    sa.lpSecurityDescriptor := @sd;
  end
  else
    sa.lpSecurityDescriptor := nil;
  sa.nLength := sizeof(SECURITY_ATTRIBUTES);
  sa.bInheritHandle := True;
  if CreatePipe(newstdin, write_stdin, @sa, 0) then
  begin
    if CreatePipe(read_stdout, newstdout, @sa, 0) then
    begin
      GetStartupInfo(si);
      with si do
      begin
        dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
        wShowWindow := SW_HIDE;

        hStdOutput := newstdout;
        hStdError := newstdout;
        hStdInput := newstdin;
      end;
      FillChar(Buffer, sizeof(Buffer), 0);
      GetEnvironmentVariable('COMSPEC', @Buffer, sizeof(Buffer) - 1);
      StrCat(@Buffer, PChar(' /c ' + Cmd));
      if CreateProcess(nil, @Buffer, nil, nil, True, CREATE_NEW_CONSOLE, nil,
        nil, si, pi) then
      begin
        Str := #13;
        WriteFile(write_stdin, PChar(Str)^, Length(Str), bread, nil);
        repeat
          PeekNamedPipe(read_stdout, @Buffer, sizeof(Buffer) - 1, @bread,
            @avail, nil);
          if bread > 0 then
          begin
            FillChar(Buffer, sizeof(Buffer), 0);
            ReadFile(read_stdout, Buffer, bread, bread, nil);
            Result := Result + string(PansiChar(@Buffer));

          end;
          GetExitCodeProcess(pi.hProcess, exitcod);
        until (exitcod <> STILL_ACTIVE) and (bread = 0);
      end;
      CloseHandle(read_stdout);
      CloseHandle(newstdout);
    end;
    CloseHandle(newstdin);
    CloseHandle(write_stdin);
  end;
end;

function IsWindows64: boolean;
type
  TIsWow64Process = function(AHandle: THandle; var AIsWow64: BOOL)
    : BOOL; stdcall;
var
  vKernel32Handle: DWORD;
  vIsWow64Process: TIsWow64Process;
  vIsWow64: BOOL;
begin
  // 1) assume that we are not running under Windows 64 bit
  result := false;

  // 2) Load kernel32.dll library
  vKernel32Handle := LoadLibrary('kernel32.dll');
  if (vKernel32Handle = 0) then
    exit; // Loading kernel32.dll was failed, just return

  try

    // 3) Load windows api IsWow64Process
    @vIsWow64Process := GetProcAddress(vKernel32Handle, 'IsWow64Process');
    if not Assigned(vIsWow64Process) then
      exit;
    // Loading IsWow64Process was failed, just return

    // 4) Execute IsWow64Process against our own process
    vIsWow64 := false;
    if (vIsWow64Process(GetCurrentProcess, vIsWow64)) then
      result := vIsWow64; // use the returned value

  finally
    FreeLibrary(vKernel32Handle); // unload the library
  end;
end;

Function InstalUinstallMTP_Driver(IsInstall: boolean = true): boolean;
var
  driverpath, dir, cmd: string;
begin
  result := false;
  driverpath := ExtractFilePath(Application.ExeName) +
    'adb\mtp\drivers\SAMSUNG_Android.inf';
  if not fileexists(driverpath) then
  begin
  adlg('please inform me for driver not found ',clblue,true)  ;
    exit;
  end;
  dir := ExtractFilePath(Application.ExeName) + 'adb\mtp\drivers';

  if IsWindows64 then
    driverpath := 'install_x64.exe'
  else
    driverpath := 'install_x86.exe';

  if IsInstall then
  begin
    cmd := driverpath + ' install "--inf=SAMSUNG_Android.inf"';
    // cmd := Shell(cmd, dir);
    if cmd.Contains('starting devices') then
      result := true;
  end
  else
  begin
    cmd := driverpath + ' uninstall "--inf=SAMSUNG_Android.inf"';
    // cmd := Shell(cmd, dir);
    // addlog(cmd);
    if cmd.Contains('starting devices') then
      result := true;
  end;
end;

 

Share this post


Link to post

Without the actual error(s), we'll be guessing.

Is it lack of process security elevation so that it fails due to insufficient rights?

Share this post


Link to post

You said the error occur in Shell function and then you showed code for Shell function... So where EXACTLY is the error produced and which error is this EXACTLY. We cannot help you if you to help us...

Share this post


Link to post
22 minutes ago, FPiette said:

You said the error occur in Shell function and then you showed code for Shell function... So where EXACTLY is the error produced and which error is this EXACTLY. We cannot help you if you to help us...

  cmd := driverpath + ' install "--inf=SAMSUNG_Android.inf"';
    // cmd := Shell(cmd, dir);
    if cmd.Contains('starting devices') then
      result := true;
  end
  else
  begin
    cmd := driverpath + ' uninstall "--inf=SAMSUNG_Android.inf"';
    // cmd := Shell(cmd, dir);
    // addlog(cmd);

cmd error

Share this post


Link to post

It is a problem that you have zero error handling around the Win32 operations in the Cmd method.

Without the precise error information from the failing API(s), we are completely in the dark.

F.x. if CreateProcess fails, you need to call GetLastError and log or pass on the error info somehow.
Almost every Win32 API method will update last error.

  • Like 1

Share this post


Link to post

You don't help yourself... Shell is a function you wrote. We cannot tell you what errors it returns. Modify your code to get the error code or error message INSIDE the Shell function, everywhere an API function is called.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×