0

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.

14
  • That looks like a COM exception, and you don't access COM functionality via LoadLibrary or GetProcAddress. What does the documentation for the DLL say? Commented Jan 12, 2018 at 21:52
  • 1
    Might want to use \\ in your paths instead of \. May not be contributing to the error, but it probably isn't helping any. Commented Jan 12, 2018 at 21:54
  • @Tibrogargan Hey, so I did as you advised and my program now runs through to my return statement! Butttt I'm getting a new error message after the return statement: Error message: Unhandled exception at 0x00000001 in RDISample1.exe: 0xC0000005: Access violation executing location 0x00000001. Commented Jan 12, 2018 at 22:06
  • Was the BSTR you're passing in freed by the callee? It's a likely scenario in COM. (i.e. Remove the SysFreeString line and see if it works) Commented Jan 12, 2018 at 22:09
  • 1
    What @KenWhite says is correct, but it's compounded by whatever library you're calling being junk code. It crashes because it couldn't open a file that doesn't exist - which means it's not checking error conditions either. The symptom you're describing is often caused by stack corruption ... and since all you do after the library call is free some memory .. it's a fair bet that's what's going on. (I'd bet the library is crashing when it's exiting). Commented Jan 12, 2018 at 22:22

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Hey, Visual Studio is not liking me adding the try-catch statement. Specifically the "_com_error" and "const" verbage. So looks like I'll have to do a little research to figure that out. In the meantime, changing \ to \\ seems to fix the immediate problem. Now my program breaks after the return message? Error Message: Unhandled exception at 0x00000001 in RDISample1.exe: 0xC0000005: Access violation executing location 0x00000001.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.