user1432966 user1432966 - 3 months ago 15
C Question

Getting response of a DLL in C

I've follow the next tutorial:

https://msdn.microsoft.com/en-us/library/64tkc9y5(v=vs.80).aspx

It is working fine for me. But I need to printf the response of the callbackfunctions:

printf(uReturnVal);


I get the error:
cannot convert argument 1 from 'UINT' to 'const char *const '


I've tried another options:

printf(Convert.ToString(uReturnVal)); //ERROR :'Convert': undeclared identifier [


And:

printf(uReturnVal->ToString()); //ERROR: '->ToString' must point to class/struct/union/generic


And:

printf(uReturnVal.ToString());//error: /ERROR: '->ToString' must point to class/struct/union/generic


This is the code of the tutorial:

typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);

HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
DWORD dwParam1;
UINT uParam2, uReturnVal;

hDLL = LoadLibrary("MyDLL");
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
"DLLFunc1");
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
return true;
}
else
{
// call the function
uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
}
}


Many thanks in advance.

Answer

First argument to printf functions is always format specifier. In your case printf("%u\n",uReturnVal) should work. See Format Specification Syntax: printf and wprintf Functions.

To Expand: The code above is pure C.

You can also do it in C++ style, even more elegant:

#include <iostream>
...
  uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
}
std::cout << "Result is: " << uReturnVal << std::endl;