CustomAction succeeds on development computer, fai

2019-07-30 09:20发布

I'm creating a WiX installer to install a program which connects to a database. To help with this, I've created a C dll which checks to see if a certain instance of SQL exists on a server:

extern "C" UINT __stdcall DBConTest(MSIHANDLE hInstaller)

{

FILE *fp;
fp = fopen("dbcontestdll.txt", "w");
_ConnectionPtr pCon;
int iErrCode;
HRESULT hr;
UINT rc;
//init COM

fwprintf(fp, L"entering dbcontest\n");
if(FAILED(hr = CoInitializeEx(NULL,tagCOINIT::COINIT_APARTMENTTHREADED)))
    return ERROR_INVALID_DATA;

fwprintf(fp,L"did coinit\n");
if(FAILED(hr = pCon.CreateInstance(__uuidof(Connection))))
    return ERROR_INVALID_DATA;

fwprintf(fp,L"created instance of connection\n");
TCHAR constr[1024];
DWORD constrlen = sizeof(constr);
rc=MsiGetProperty(hInstaller,TEXT("DBCONNECTIONSTRING"), constr, &constrlen);

fwprintf(fp, L"dbconstring is: %s\n", constr);
TCHAR serverstr[1024];
DWORD serverstrlen = sizeof(serverstr);
rc = MsiGetProperty(hInstaller,TEXT("SQLINSTANCE"),serverstr,&serverstrlen);

fwprintf(fp, L"SQLINSTANCE is: %sl\n",serverstr);
TCHAR finalconstr[2048];
swprintf(finalconstr,L"%s; Data Source=%s;",constr,serverstr);
try{
    hr = pCon->Open(finalconstr,TEXT(""),TEXT(""),adConnectUnspecified);
}
catch(_com_error ce){

    fwprintf(fp, L"%s\n", msg);
    ::MessageBox(NULL,msg,NULL,NULL);
    CoUninitialize();
    MsiSetProperty(hInstaller,TEXT("DBCONNECTIONVALID"),TEXT("0"));
    return ERROR_SUCCESS;

}
if(FAILED(hr)){
    MsiSetProperty(hInstaller,TEXT("DBCONNECTIONVALID"),TEXT("0"));
    return ERROR_SUCCESS;

}
pCon->Close();
CoUninitialize();
MsiSetProperty(hInstaller,TEXT("DBCONNECTIONVALID"),TEXT("1"));
::MessageBox(NULL,TEXT("Successfully connected to the database!"),NULL,NULL);
fwprintf(fp, L"leaving...\n");
fclose(fp);
return ERROR_SUCCESS;

}

Now, when I build this function into a dll and add it to my WiX project, this code works on my development machine (specifically, the installation successfully finishes and the file "dbcontestdll.txt" exists and has the correct data in it)--but, when I run it on a "fresh install" machine, the installation fails with exit code 2896 and the "dbcontestdll.txt" is not created.

Are there prerequisites to using C-based dlls in a Windows Installer, such as the C++ redistributable?

4条回答
SAY GOODBYE
2楼-- · 2019-07-30 09:33

I had this problem also. I had an MFC DLL that was dynamically linking by default, and I forgot to include MSVCR100.DLL in the package. Of course it worked fine on the development machine, it even worked on most customers' machines, but it failed on an old Vista PC. I switched to statically linked.

查看更多
仙女界的扛把子
3楼-- · 2019-07-30 09:39

Yes you probably need the visual c runtime. Dependency Walker might assist finding the required dlls.

Look at this example how to use a Bootstrapper. This way you can install the runtime before the msi will be run. I use the following bootstrapper line:

<BootstrapperFile Include="Microsoft.Visual.C++.9.0.x86">
  <ProductName>Visual C++ 2008 Runtime Libraries (x86)</ProductName>
</BootstrapperFile>

This package is normally stored in the C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\vcredist_x86 directory.

查看更多
成全新的幸福
4楼-- · 2019-07-30 09:54

For custom actions, I highly recommend statically linking to the C run time. The custom aciton DLL ends up a little bigger but you'll have one less dependency on files outside the custom action.

查看更多
甜甜的少女心
5楼-- · 2019-07-30 09:58

You probably don't want to get yourself into the situation where you have to bootstrap C++ redists just to run a Custom Action. Have you tried using the File | New | C++ Custom Action probject that comes with WiX? You can use that to stub out your CA and then copy and paste your code into it. That should give you all the compiler and linker settings that you need to avoid this problem.

查看更多
登录 后发表回答