You say that when you call your code from CVI it works. In testing your code outside of TestStand, you may want to do so in a manner that most simulates calling your code from TestStand. As Azucena implied, this means that within your standalone application you should execute your functions on a separate thread that is coinitialized as multithreaded. It could be that your code will not execute on a thread that is coinitialized as multithreaded. I have included some test VC and CVI code that I used at some point to simulate TestStand calling a DLL. Excuse any errors or non-standard usage.
If you get the same error I would then recommend you comment out the CoinitializeEX function. This will tell you whether your code is trying to coinitialize has something other than multithreaded mode.
Hope this helps.
(You will have to fix the wrapping of the code.)
===================
VC Code:
#include "stdafx.h"
#include
#include
#import "C:\\TEMP\\YourServer.dll" no_implementation
int main(int argc, char* argv[])
{
HANDLE hThread = NULL;
DWORD error =0;
DWORD threadId;
DWORD tmpExitCode = 0;
LPLONG lpExitCode;
//Function called in the main thread of the application is NOT the manner
in which TS calls DLL functions. In your
//current application it sounds like this is working.
tmpExitCode = myThreadFunc(NULL);
//Function called on a separate thread coinitialized as multithreaded is
how TS calls DLL functions. This may not
//work with your current application.
hThread = CreateThread (0, 0, myThreadFunc, NULL, 0, &threadId);
if (hThread)
{
WaitForSingleObject (hThread, INFINITE);
GetExitCodeThread(hThread, &exitCode);
CloseHandle (hThread);
}
lpExitCode = (long) tmpExitCode;
return *lpExitCode;
}
DWORD WINAPI myThreadFunc(LPVOID lpParameter)
{
HRESULT __result;
IYourServerPtr *mycallmgr;
CLSID clsid = __uuidof(YourServer);
IID iid = __uuidof(IYourServer);
CoInitializeEx(NULL, COINIT_MULTITHREADED);
//YOUR CODE SUCH AS:
__result = CoCreateInstance(
clsid,
NULL,
CLSCTX_SERVER ,
iid,
(void **) &mycallmgr);
Initialize ();
Test ();
Cleanup();
CoUninitialize();
return __result;
}
======================
CVI Code:
#include
#include "MyServer.h"
#include
#include
#include "toolbox.h"
#include
#include
#include
DWORD WINAPI myThreadFunc(LPVOID lpParameter);
static CAObjHandle MyServer=0;
static char errorString[1024];
int main (int argc, char *argv[])
{
int error = 0;
LPVOID lpParameter; // thread argument
LPDWORD lpThreadId; // thread identifier
HANDLE threadHndl;
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
//Function called in the main thread of the application
//is NOT the manner in which TS calls DLL functions.
//In your current application it sounds like this is working.
errChk(myThreadFunc(NULL));
//Function called on a separate thread coinitialized as
//multithreaded is how TS calls DLL functions. This may not
//work with your current application.
threadHndl=CreateThread(
NULL, // SD
0, // initial stack size
myThreadFunc, // thread function
lpParameter, // thread argument
0, // creation option
NULL // thread identifier
);
if (threadHndl)
errChk(WaitForSingleObject (threadHndl, INFINITE));
Error:
// free all resources
if (error<0)
MessagePopup ("error occurred", "");
return error;
}
DWORD WINAPI myThreadFunc(LPVOID lpParameter){
int error = 0;
ERRORINFO errorInfo;
errChk( CoInitializeEx(NULL, COINIT_MULTITHREADED));
//Create my server object
errChk(MyServerLib_NewIMyServer (NULL,1, LOCALE_NEUTRAL, 0,
&MyServer));
CoUninitialize( );
Error:
// free all resources
CA_DiscardObjHandle(MyServer);
if (error<0)
CA_GetAutomationErrorString (error, errorString, 1024);
MessagePopup ("error occurred", errorString);
return error;
}