-
Calling a DLL
Hi
I'm using Visual Studio 2005. I have a simple Win 32 console app that I'm trying to explicitly call a 3rd party dll from.
I can call the dll if I save my file that calls the dll as a "C" program. However, it does not work if I try to save it as a "CPP" file. I need to save it as a cpp file. I have two questions:
First, is it possible to save the file in C and than call the function in a CPP program. The second question shows what I've tried along with the error I'm getting:
I've placed the dll and the library in my system 32 folder. Here's what I have:
Code:
// test1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
char cSmilesPass[25];
char cChemical[1000];
char EstLin[1000];
char EstNon[1000];
char EstUlt[1000];
char EstPrim[1000];
char UltTime[25];
char PrimTime[25];
char EstMitiLin[1000];
char EstMitiNon[1000];
char DetailResults[12000];
char numLines[1000];
char ErrorMess[12000];
int CallBIOWDLL(void)
{
/* get handle to dll */
HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\WINDOWS\\system32\\BIOWDLL.dll");
/* get pointer to the function in the dll*/
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL), "GetSrcBIOWIN");
/* Define the Function in the DLL for reuse. This is just prototyping
the dll's function. A mock of it. Use "stdcall" for maximum compatibility.
*/
typedef int (__stdcall * pICFUNC)(char*,char*,char*,char*,char*,char*,char*,char*,char*,char*,char*,char*,char*);
pICFUNC MyFunction;
MyFunction = pICFUNC(lpfnGetProcessID);
/* The actual call to the function contained in the dll */
int intMyReturnVal = MyFunction("test","test2",EstLin,EstNon,EstUlt,EstPrim,UltTime,PrimTime,EstMitiLin,EstMitiNon,DetailResults,numLines,ErrorMess);
/* Release the Dll */
FreeLibrary(hGetProcIDDLL);
/* The return val from the dll */
return intMyReturnVal;
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
When I try to compile, I get the following error:
Compiling...
test1.cpp
c:\documents and settings\epajn\my documents\visual studio 2005\projects\test1\test1\test1.cpp(42) : error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'const char [32]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Build log was saved at "file://c:\Documents and Settings\epajn\My Documents\Visual Studio 2005\Projects\test1\test1\Debug\BuildLog.htm"
test1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I've been struggling with this for way too long. I will be extremely grateful for any pointers.
--------------------------------------------------------------------------------
James
-
Figured it out
The LPCWSTR in the error message implies that you compile your code for unicode, so the functions expect wide chars instead of chars. There're two ways to fix this error: the first is to use L"Foo" instead of "Foo" for each string literal (or for strings that need to be wide chars irrelevant of compiling mode). The other possibility is to use _T("Foo") which will prefix the literal with L modifier if required.