Ruben
Ruben

Reputation: 154

Calling dll from a Win32 Console Application [C]

Hi everyone and thank you for your time. This was created in Visual Studio 2012,and I'm using the standard Windows Libraries.

I am attempting to call a DLL function explicitly and I believe the code I've written is correct; however, I am receiving an error. I'm not sure if it's an error in something that I've written in the small C console application or from the DLL which I do not have access to the internal workings of.

//global area

HINSTANCE _createInstance;
typedef UINT (CALLBACK* LPFNDLLFUNCLOOKUP)(AccuInput*, AccuOut*);
LPFNDLLFUNCLOOKUP lpfnDllFuncCASSLookup;
typedef UINT (CALLBACK* LPFNDLLFUNCINIT)(BSTR);
LPFNDLLFUNCINIT lpfnDllFuncInit;
typedef UINT (CALLBACK* LPFNDLLFUNCCLOSE)(); 
LPFNDLLFUNCCLOSE lpfnDllFuncClose;
HMODULE unmanagedLib;

Here is my main function:

int main() {

// Load Library
BSTR configFile;
unmanagedLib = LoadLibraryA((LPCSTR) "AccuAddressUnMgd.dll");
// Initialize AccuAddress COM dll
lpfnDllFuncInit = (LPFNDLLFUNCINIT)GetProcAddress(unmanagedLib, (LPCSTR)"Init");
// This function will lookup the address
lpfnDllFuncCASSLookup = (LPFNDLLFUNCLOOKUP)GetProcAddress(unmanagedLib, (LPCSTR)"AccuCassLookup");
// This function will call AccuAddress COM DLL Close function
lpfnDllFuncClose = (LPFNDLLFUNCCLOSE)GetProcAddress(unmanagedLib, (LPCSTR)"Close");
// Append “config.acu” file path.
configFile = SysAllocString(L"C:\PathTo\Config.acu");
printf("ConfigPath created");
lpfnDllFuncInit(configFile);
printf("ConfigFile consumed");
SysFreeString(configFile);
return 0;
}

This is the error that I receive:

Unhandled exception at at 0x75D4C54F in RDISample1.exe: Microsoft C++ exception: _com_error at memory location 0x001AFAC0.

The error occurs at:

lpfnDllFuncInit(configFile);

So, I guess my question is two parts. Based off the code can I say for a fact that the error is in the DLL function?

Second question, when calling GetProcAddress what would be the point (if any) for encapsulating the string in LPCSTR like a function versus typecasting? ie

lpfnDllFuncClose = (LPFNDLLFUNCCLOSE)GetProcAddress(unmanagedLib, LPCSTR("Close"));

Thanks again for the help. I've been doing a fair amount of research yet DLLs still have been puzzled.

Upvotes: 0

Views: 771

Answers (2)

Tibrogargan
Tibrogargan

Reputation: 4603

The initial error is caused by the library you're using failing to correctly handle a file that doesn't exist.

The path you gave contains single slashes \, which are treated as escape characters, not path separators. Path separators must be escaped, i.e. \\ to be treated correctly.

Upvotes: 1

SoronelHaetir
SoronelHaetir

Reputation: 15162

There is no point casting a string literal to LPCSTR.

As for the _com_error that is definitely coming from the DLL. I would suggest wrapping that in a:

try
{
  ...
} catch(_com_error const & e)
{
  wprintf(L"Caught a com error: %s\r\n", e.ErrorMessage());
}

And then you might be able to figure out what is wrong.

Upvotes: 0

Related Questions