根据不同的操作系统(64/32),设置文件以64位运行。又可解决问题:“试图加载不正确的程序”。

时间:2022-04-21 00:18:24
//button事件

void CSqlConTestDlg::OnOK()

{

         CString strPath = "cmd.exe";

         if(IsWow64())

         {

                   Wow64DisableWow64FsRedirection(NULL);

         }

         STARTUPINFO si = {sizeof(STARTUPINFO)};

         ZeroMemory(&si,sizeof(STARTUPINFO));

         si.cb = sizeof(STARTUPINFO);

         si.wShowWindow = SW_SHOW;

         si.dwFlags = STARTF_USESHOWWINDOW;

         PROCESS_INFORMATION pi;

         memset(&pi,0,sizeof(pi));

         if(CreateProcess(NULL,(char*)(LPCSTR)strPath,NULL,NULL,false,0,NULL,NULL,&si,&pi))

         {  

         }

         //阻塞上一进程

         while (WaitForSingleObject(pi.hProcess,20))

         {

                   ;

         }

         Wow64RevertWow64FsRedirection(NULL);

}

 

/****************************************************/

/*判断是否是64位系统                                                                 */

/****************************************************/

typedef BOOL (WINAPI *LPEN_ISWOW64PROCESS)(HANDLE,PBOOL);

BOOL CSqlConTestDlg::IsWow64()

{

         BOOL bIsWow64 = false;

         LPEN_ISWOW64PROCESS fnIsWow64Process;

         fnIsWow64Process = (LPEN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");

         if(NULL != fnIsWow64Process)

         {

                   if(!fnIsWow64Process(GetCurrentProcess(),&bIsWow64)){};

         }

         return bIsWow64;

}

 

/****************************************************/

/*如果系统是64位,取消文件的重点位                                         */

/****************************************************/

typedef BOOL (WINAPI *LPEN_WOW64DISABLEWOW64FSREDIRECTION)(PVOID* OldValue);

BOOL CSqlConTestDlg::Wow64DisableWow64FsRedirection(PVOID* OldValue)

{

         BOOL bRet = false;

         LPEN_WOW64DISABLEWOW64FSREDIRECTION fnWow64DisableWow64FsRedirection;

         fnWow64DisableWow64FsRedirection = (LPEN_WOW64DISABLEWOW64FSREDIRECTION)

                   GetProcAddress(GetModuleHandle(TEXT("kernel32")),"Wow64DisableWow64FsRedirection");

         if(NULL != fnWow64DisableWow64FsRedirection)

         {

                   if(fnWow64DisableWow64FsRedirection(OldValue))

                   {

                            bRet =      true;

                   }

         }

         return bRet;

}

 

/****************************************************/

/*如果系统是64位,恢复文件的重点位                                         */

/****************************************************/

typedef BOOL (WINAPI *LPEN_WOW64REVERTWOW64FSREDIRECTION)(PVOID OldValue);

BOOL CSqlConTestDlg::Wow64RevertWow64FsRedirection(PVOID OldValue)

{

         BOOL bRet = false;

         LPEN_WOW64REVERTWOW64FSREDIRECTION fnWow64RevertWow64FsRedirection;

         fnWow64RevertWow64FsRedirection = (LPEN_WOW64REVERTWOW64FSREDIRECTION)GetProcAddress(GetModuleHandle(TEXT("kernel32")),"Wow64RevertWow64FsRedirection");

         if(NULL != fnWow64RevertWow64FsRedirection)

         {

                   if(fnWow64RevertWow64FsRedirection(OldValue))

                   {

                            bRet = true;

                   }

         }

         return bRet;

}