Reputation: 1693
Alright guys this is an extension on my last post which was solved and that part is working great (link below)
Not finding function using GetProcAddress() C++ VBexpress 13
unfortunately another area of misunderstanding came about. below is the code i'm about to reference:
#include "stdafx.h"
#include <iostream>
#include "Windows.h"
#include <stdio.h>
typedef int(__cdecl *MYPROC)(LPWSTR);
using namespace std;
int main()
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
hinstLib = LoadLibrary(TEXT("testDLL.dll"));
if (hinstLib != NULL)
{
ProcAdd = (MYPROC)GetProcAddress(hinstLib, "?Add@MyMathFuncs@MathFuncs@@SANNN@Z");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
c=(ProcAdd)(L"something here");
}
fFreeResult = FreeLibrary(hinstLib);
}
return 0;
}
The issue: having trouble interfacing with the functions. the program recognizes the DLL and the function. I'm sure it has something to do with the typedef, the assignment to ProcAdd, and my actual call of the function. In this example i'm calling a function that adds doubles together. Obviously i need to pass 2 doubles. it seems like logic would dictate i could replace the typedef with 'typedef int(__cdecl *MYPROC)(double,double);' or something similar and replace the L"something here" with 2 doubles and assign it to a value. The are no runtime errors when i do this but i just gives a large negative number for the returned number. What exactly are happening in these 2 lines that are throwing me? i'm not even sure what to specifically unfortunately. i understand what _cdecl is.
Short background: i'm having to interface with a DLL that i dont have a .lib file for. I was having trouble so i made a DLL with the MS tutorial at http://msdn.microsoft.com/en-us/library/ms235636.aspx and am referencing that DLL with the code above, which was taken from another MS tutorial if i remember correctly.
Any help in understanding these basic concepts would be greatly appreciated. thank you!
Upvotes: 3
Views: 9387
Reputation: 2508
typedef int(__cdecl *MYPROC)(LPWSTR);
is introducing a type definition for a function pointer, in words it translates to:
"MYPROC is a pointer to a function that takes a LPWSTR and returns an int". So your assumption is correct. Your logic is also correct, in that you want to do something along the lines of:
typedef double(__cdecl *MYPROC)(double, double);
....
MYPROC pMyFun = (MYPROC)GetProcAddress(hinstLib, "?Add@MyMathFuncs@MathFuncs@@SANNN@Z");
....
pMyFun(1.0,2.0);
What this is doing, is introducing a new type in the first line. We then declare a variable of this type and then assign it the address of the function of interest and then finally, we call the function by using this pointer. If you are new to function pointers this tutorial may help
Given that, I have two questions:
(1) where did you get the name for the function? (2) is TestDll.dll the dll you wrote, or the one that you need to interface to?
Upvotes: 8