Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I've a vba script calling a dll function. It works. I'd like to convert into C program. I use LoadLibrary/GetProcAddress, they return non null value, ok. But when I call my new function, there is a Windows execution error.
Maybe problem with the convention call, or the parameters format : I don't have any prototype, only the vba script call.

VBA :
VB
option explicit
declare function myfun lib "mylib" (byval p1$, p1len as integer, byval p2$, p2len as integer, byval p3$, p3len as integer) as integer
type mytype
  p1 as string : p1len as integer
  p2 as string : p2len as integer
  p3 as string : p3len as integer
end type
sub main
  dim myvar as mytype
  dim p1len as integer, p2len as integer, p3len as integer
  dim p1$, p2$, p3$
  let mytype.p1 = string$(255, " ") : let mytype.p1len = 255
  let mytype.p2 = string$(255, " ") : let mytype.p2len = 255
  let mytype.p3 = string$(255, " ") : let mytype.p3len = 255
  ret = myfun(mytype.p1, mytype.p1len, mytype.p2, mytype.p2len, mytype.p3, mytype.p3len)
end sub

C :
C++
typedef DWORD __cdecl (*GLIX)(char *p1, DWORD p1len, char *p2, DWORD p2len, char *p3, DWORD p3len);
void main(void)
{
HMODULE hm = LoadLibrary(TEXT("C:\\mylib.dll"));
GLIX myfun = (GLIX)GetProcAddress(hm, "myfun");
char *p1, *p2, *p3;
strcpy(p1, "                ");
strcpy(p2, "                ");
strcpy(p3, "                ");
DWORD p1len = 255, p2len = 255, p3len = 255;
myfun(p1, p1len, p2, p2len, p3, p3len); // ko here
}

I've tried another size than DWORD, or __stdcall, same problem.
Any idea, please ? Thanks.
Jean-Claude
Posted
Comments
Richard MacCutchan 4-Oct-15 2:58am    
What error?
Member 12030981 4-Oct-15 9:06am    
I run it under console window, then Windows displays "the program has stopped working, a problem causing the program to fail. close the program." (sorry for the translation, I've it in french) Not really clear.
Richard MacCutchan 4-Oct-15 9:58am    
You need to use your debugger to trace what is happening, the informastion above is not enough to guess what is going wrong. If you have the source code for the DLL that should also help.
Member 12030981 4-Oct-15 10:56am    
I don't have any source code for the DLL, neither any prototype. I'm not sure how to call the function, except using the same parameters like in vba script.
If I run my C program in CodeBlock, the call is stopped with "SIGSEGV, segmentation fault" : I can look at the assembly code (of my program, not DLL), but it doesn't help me, except a curious thing : the DLL contains 4 functions, I want to use "myfun", but the call stack of CB shows "SyncDLL!GetUserId" (another function of the DLL).
Richard MacCutchan 4-Oct-15 11:11am    
See my update below.

1 solution

There are a couple of issues with your code (which I missed before). You create three character pointers, but never point them at anything, so when you try to copy the spaces into them the segmentation fault occurs. You are also trying to tell the DLL that each buffer contains 255 characters when there are only 17. I would suggest the following changes:
C++
char *p1, *p2, \*p3;
p1 = (char*)malloc(255);  // allocate a buffer of 255 characters
memset(p1, ' ', 255);     // and fill with spaces
p2 = (char*)malloc(255);
memset(p2, ' ', 255);
p3 = (char*)malloc(255);
memset(p3, ' ', 255);
myfun(p1, 255, p2, 255, p3, 255); // 
 
Share this answer
 
v2
Comments
Richard MacCutchan 4-Oct-15 11:56am    
Note: there should be an asterisk in front of p3, and after char in line 2, i.e. it should be
char *p1, *p2, *p3;
p1 = (char*)malloc(255);
It seems that the phantom markdown gremline is at work again.
Member 12030981 4-Oct-15 12:11pm    
No, sorry, I've forgot the line here by anonymizing the code : I used char p1[255], p2[255], p3[255]. I've also tried with char *p1 and p=malloc...
Always the same error at the call.
But, forget my C example. How would you translate my VBA script into C program ? (nb: another error here, I wrote "mytype" instead of "myvar" for using this variable)
Richard MacCutchan 4-Oct-15 12:19pm    
I cannot answer that without knowing the exact details of the DLL function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900