Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing an LPSTR to a dll function call

Status
Not open for further replies.

converge

Programmer
Jul 10, 2002
15
US
i'm passing an LPSTR to a c++ dll function call from a c++ exe application. but i'm getting an exception thrown when i try to do this. here is my code:

LPSTR sInstalledProducts;
int* dummy;

typedef int (__stdcall FUNCPROC)(HWND, LPSTR, int*);

HINSTANCE hInstance;
FUNCPROC* pFunction;

VERIFY(hInstance = ::LoadLibrary(sSysDir));
VERIFY(pFunction = (FUNCPROC*)::GetProcAddress(hInstance, "Find_Installed_Products"));

int i = (*pFunction)(this->GetSafeHwnd(), sInstalledProducts, dummy);


What am I doing wrong? My problem has something to do with sInstalledProducts.
 
I don't know how the DLL function works, but I can see this:

A function that's prototyped as Func(HWND,LPSTR,int *) is using its parameters to return information back to the caller. It's expecting to be able to write information back to the second and third parameters.

I don't know how you're supposed to know how many items there are before you pass in the string, but if there's a way to find out, then you need to find out how much space you need first, allocate the space, and then pass in the string. (Remember, an LPSTR is really just a char *.)

Or, it could be the last parameter. You did the same thing. It's possible the DLL allocates the space for the string (I don't know how it works), but it's definitely expecting to be able to write something back to that int *. You declared an int * and passed it in to the function without allocating space for it. The way parameters like this are usually passed in is:

Code:
int x;
MyFunc(&x);

Remember, when you use pointers, you must allocate their memory before you use them, and release it when you finish.
 
dds82 has right.
Do so:
LPSTR sInstalledProducts[256]; //Alllocate some memory
LPSTR pInstalledProducts = sInstalledProducts; //That is needed, if DLL allocates memory itself
int dummy; //Do not use pointer here

... //Your old code
int i = (*pFunction)(this->GetSafeHwnd(), pInstalledProducts, &dummy);
 
thanks, i used this to get it to work:

LPSTR sInstalledProducts;
TCHAR foo[100];
sInstalledProducts = foo;

int i = (*pFunction)(this->GetSafeHwnd(), sInstalledProducts, &dummy);
 
Also, for reference, you don't need to dereference function pointers to call them.

(*pFunction)(...) works just as well as pFunction(...).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top