Hi, I am trying to use a method defined in a DLL Project, say, "Project A" and use it in "Project B".
The method's declaration, in Project A's header file, is:
|
extern "C"{__declspec(dllexport) matrix __cdecl getMatrixFromSubFile(setInt rows, std::string fn, int factor);}
|
,
and, it's implementation, defined in Project A's cpp.file, is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
extern "C"{
__declspec(dllexport) matrix __cdecl getMatrixFromSubFile(setInt rows, std::string fn, int factor)
{
matrix ma;
std::ifstream *f = new std::ifstream(fn);
char a[10000];
for (int i = 0; i < rows.intSet.size(); i++)
{
for (int j = 0; j <rows.intSet.at(i)*factor; j++)
{
if (j == rows.intSet.at(i)*factor -1)
{
f->getline(a, 10000);
std::string szVec = std::string(a);
avector curvec = szVectorToRealVec(szVec);
ma.vectors.push_back(curvec);
//f = new std::ifstream(fn);
f->seekg(0, f->beg);
}
else
{
f->getline(a,10000);
}
}
}
f->close();
delete f;
return ma;
}
}
|
.
Now, In "Project B", I go about getting an HINSTANCE handle to the DLL as follows:
|
HINSTANCE hGetProcIDDLLLINEAR_ALGEBRA = LoadLibrary(L"C:\\Users\\a\\Documents\\visual studio 2012\\Projects\\DLLs\\LINEAR_ALGEBRA\\LINEAR_ALGEBRA\\Debug\\LINEAR_ALGEBRA.dll");
|
This works.
Then, in the same file, further ownward in code, I do:
1 2 3 4 5 6 7 8 9 10 11
|
FARPROC lpfnGetProcessID;
lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLLLINEAR_ALGEBRA),"getMatrixFromSubFile");
std::cout << GetLastError() << std::endl;
/*
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 matrix (__cdecl * pICFUNCGet_Matrix_From_Sub_File)(setInt rows, std::string fn, int factor);
pICFUNCGet_Matrix_From_Sub_File get_matrix_from_sub_file_P;
get_matrix_from_sub_file_P = pICFUNCGet_Matrix_From_Sub_File(lpfnGetProcessID);
|
This usually works. However, in this case, line:
|
lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLLLINEAR_ALGEBRA),"getMatrixFromSubFile");
|
returns a NULL pointer, and the next line:
|
std::cout << GetLastError() << std::endl;
|
"couts"
"127" (i.e. error 127).
When compiling Project A (the DLL), I get a compiler warning:
1>c:\users\a\documents\visual studio 2012\projects\dlls\linear_algebra\linear_algebra\linear_algebra\LINEAR_ALGEBRA.h(41): warning C4190: 'getMatrixFromSubFile' has C-linkage specified, but returns UDT 'matrix' which is incompatible with C
|
So, I tried using "C++" linkage, instead. This eliminates the compiler warning in Project A, but the "result" in "Project B" is, unfortunately, unchanged: Above line:
|
lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLLLINEAR_ALGEBRA),"getMatrixFromSubFile");
|
STILL returns a NULL pointer, and the error "127" is "couted".
Why is this happening, and, how can I work around it, so that I can use a DLL function from Project A in Project B, even though the function returns a User-Defined-Type ("matrix")? (i.e. incompatible with "C"-linkage, apparently).
This is on Windows 8, using Visual Studio 2012 IDE.
Thanks for any and all help, and explanations!
Thanks! :)
Hansaa :)