views:

44

answers:

3

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?

A: 

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.

Wimmel
+2  A: 

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.

Christopher Painter
+1  A: 

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.

Rob Mensching
I ended up using this solution. I used the /MT switch instead of the /MD switch.
David