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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with exporting to a dll

Status
Not open for further replies.

namooth

Technical User
Dec 31, 2006
21
IL
Hey all,
I'm trying to export some of my functions to dll and im having some trouble.

how i created my dll and lib:

Code:
// Define DllExport to declare exported symbols.
#define DllExport __declspec( dllexport )
 DllExport __declspec( dllexport )
 
 

#include <stdio.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include  <commctrl.h>

typedef struct
{
    char secnum[8];
    char xnum[8];
    char xfirst[10];
    char xlast[10];
    char xage[3];
    char xaddress[10];
    char xphone[10];
}Item;

typedef struct phonebook  
{
    char num[4];
    char first_name[10];
    char last_name[10];
    char age[3];
    char address[10];
    char phone[10];
    
} PB;
 
DllExport int adde (int x, int y)
{
    return  (x+y);
}

DllExport int Refresh(FILE *f1,Item ListItem[])
{

  char str2[4];
  int x=0,y=1;
  PB p;
  rewind(f1); 
  
   while  (fread(&p, sizeof(PB), 1, f1)) 
                       {
                         itoa (y,str2,10);
                         strcpy(ListItem[x].secnum,p.num);
                         strcpy(ListItem[x].xnum,str2);
                         strcpy(ListItem[x].xfirst,p.first_name);
                         strcpy(ListItem[x].xlast,p.last_name);
                         strcpy(ListItem[x].xage,p.age);
                         strcpy(ListItem[x].xaddress,p.address);
                         strcpy(ListItem[x].xphone,p.phone);
                         x++;
                         y++;
                        }                                       
  return x;
}

my def file:
Code:
EXPORTS
adde
Refresh

my c file:

Code:
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     {
......
int t=-1;
typedef int  (CALLBACK *MYPROC)(int);
typedef int  (CALLBACK *MYPROC2)(FILE*, Item*);
HINSTANCE hinstLib; 
MYPROC ProcAdd; 
MYPROC2 ProcAdd2; 
int fRunTimeLinkSuccess;

....
}

          case WM_COMMAND :
.......
 			  hinstLib = LoadLibrary("MyProj.dll");

			  if (hinstLib != NULL) 
				{ 
    
					ProcAdd = (MYPROC) GetProcAddress(hinstLib, "adde");
					if (fRunTimeLinkSuccess = (ProcAdd != NULL)) 
					bla = ProcAdd (4,4); // Works! bla is now 8
					FreeLibrary(hinstLib); 
.......
	hinstLib = LoadLibrary("MyProj.dll");
              if (hinstLib != NULL) 
                { 
                    ProcAdd2 = (MYPROC2) GetProcAddress(hinstLib, "Refresh");
                    if (fRunTimeLinkSuccess = (ProcAdd2 != NULL)) 
                    t=ProcAdd2 (poi,ListItem); // Doesn't work
                    FreeLibrary(hinstLib); 

}


The function "adde" works fine so im guessing the dll is ok but "Refresh" has a problem.
i don't know if
Code:
t=ProcAdd2 (poi,ListItem);
isn't being executed or that nothing is being returned to "t".
For some reason i can't debug it , as if the code stops at the line
Code:
t=ProcAdd2 (poi,ListItem)

I can debug before that line but if for example i add this line:
Code:
MessageBox(hwnd, "Test", "Test", MB_OK);
after
Code:
 t=ProcAdd2 (poi,ListItem);
I won't see the popup.

Thanks
 
You can put debug (MessageBox(hwnd, "Test", "Test", MB_OK);) in the code of the Dll to see where it doesn't work.
poi is initialized ?
 
yes, poi is initialized .
Refresh works fine without the dll so it's something i did wrong on the way.. the "adde" functions works great in the dll so it's not the dll build.
 
How do you initialized ListItem ?
What is its size ?
 
I've tried to find what's wrong in the last 12 hours.. here's my code:


how i created the dll & lib:

I'm trying to return a value to "t" in line 540:
Code:
t = ProcAdd2 (poi,ListItem);

My code is linked to the lib file and the adde function works in the dll.
I've tried to remove the LoadLibrary completly and call the function like this:
Code:
t=Refresh(poi,ListItem);
but that gave me other errors.. though i would love to use that method instead of creation new MYPROC all the time.

Thanks
 
Your code is potentially dangerous.
You should use strncpy(ListItem[x].secnum,p.num, sizeof( ListItem[x].secnum)); instead of strcpy(ListItem[x].secnum,p.num);

Did you test your code in a simple console program, not in a dll ?
 
I tested your program on Visual Studio 2008

I made changes :
Code:
typedef int   (*MYPROC)(int, int);
typedef int   (*MYPROC2)(FILE*, Item*);
You had a problem between "__declspec( dllexport )" and CALLBACK.

I change also, as I told you,
Code:
     strncpy(ListItem[x].secnum,p.num, sizeof(p.num));
     strncpy(ListItem[x].xnum,str2, strlen(str2));
     strncpy(ListItem[x].xfirst,p.first_name, sizeof(p.first_name));
     strncpy(ListItem[x].xlast,p.last_name, sizeof(p.first_name));
     strncpy(ListItem[x].xage,p.age, sizeof(p.age));
     strncpy(ListItem[x].xaddress,p.address, sizeof(p.address));
     strncpy(ListItem[x].xphone,p.phone, sizeof(p.phone));
and it works !

The data file was initialized with
Code:
	PB p[] = {  {"001", "first1   ", "martin   ", "29", "123456789", "123456789"},
				{"002", "first2   ", "martin   ", "28", "123456789", "123456789"},
				{"003", "first3   ", "martin   ", "52", "123456789", "123456789"},
				{"004", "first4   ", "martin   ", "43", "123456789", "123456789"}};

	poi = fopen("C:\\Travail\\Developpements\\Visual Studio\\C\\TestDll\\toto.txt", "w");

	fwrite(p, sizeof(PB), 4, poi);

	fclose(poi);
 
doesn't work for me :(
i made your changes
Code:
 typedef int   (*MYPROC)(int, int);
typedef int   (*MYPROC2)(FILE*, Item*);
in code.c

and the other ones in the cpp file, created the dll & lib, moved them to my project but when i press on delete stull nothing happens.
Have u managed to run the c code and to see the the ListView is updating live after u click the button "Delete"?
 
btw,
if i leave the code under WM_COMMAND i get this error:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
 
I must say that I did a C program, not C++ and it works in console mode not GUI (that why you used CALLBACK I think), I used the Dll not the lib (but for that, I don't think this change anything).
 
i also use C and 2008 but with gui, and when i press Add i get the error above plus the refresh doesn't work for me
 
I'll try your code later, it's time to lunch (8 pm in France).
 
I tried your code, the difference with your project is that the code of the Dll was a C code (.c not .cpp).
I had to remove CALLBACK from the typedef of MYPROC and MYPROC2 in code.c.

I must say that I don't understand your code : why did you write a dll to fill the ListItem ? sometimes you use it and sometimes you don't.

I found something very strange and I can't explain, in code.c sizeof(Item) is 108 and in the Dll it's size is 59 that's why Refresh don't work.
 
I just found why, in code.c you have
Code:
typedef struct
{
	int  secnum[8];
	int  xnum[8];
	char xfirst[10];
	char xlast[10];
	char xage[3];
	char xaddress[10];
	char xphone[10];
}Item;
and in PyProj.cpp you have
Code:
typedef struct
{
    char  secnum[8];
    char  xnum[8];
    char xfirst[10];
    char xlast[10];
    char xage[3];
    char xaddress[10];
    char xphone[10];
}Item;
Read the first two lines of the struct !!!
 
Thanks for your help.
I changed the int in the first two lines to chat but im still getting "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention. " when i run the code. When i start delete i still don't see the refresh in action:
"First-chance exception at 0x77a08c39 in BtnLook.exe: 0xC0000005: Access violation writing location 0x00000014.
The program '[6900] BtnLook.exe: Native' has exited with code 0 (0x0).
 
It's just calling convention, in your C program, test with and without CALLBACK in
typedef int (CALLBACK *MYPROC)(int);
typedef int (CALLBACK *MYPROC2)(FILE*, Item*);
and it should work.
 
i changed it again to:
typedef int (CALLBACK *MYPROC)(int, int);
typedef int (CALLBACK *MYPROC2)(FILE*, Item*);

im still getting the error "First-chance exception at 0x77a08c39 in BtnLook.exe: 0xC0000005: Access violation writing location 0x00000014.
The program '[5412] BtnLook.exe: Native' has exited with code 0 (0x0)."
when i press delete and nothing happens...
 
That's other thing, I told you that your code is potentially dangerous, change all your
strcpy(ListItem[x].xfirst,p.first_name);
in
strncpy(ListItem[x].xfirst,p.first_name, sizeof(p.first_name));

Maybe this can fix the bug.
You can use debug mode to find where it's happen.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top