Jump to content

Help with exporting to a dll

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Shasoosh

Shasoosh

    Newbie

  • Members
  • Pip
  • 2 posts
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:

// 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:
EXPORTS

adde

Refresh

 

my c file:


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
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
t=ProcAdd2 (poi,ListItem)


I can debug before that line but if for example i add this line:
MessageBox(hwnd, "Test", "Test", MB_OK);

after
 t=ProcAdd2 (poi,ListItem);

I won't see the popup.

Thanks

#2
Ewe Loon

Ewe Loon

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
I would agree with your assessment the problem is in the Refresh function,
from what i can see there is no definition of "ListItem" so the Refresh function will be trying to load the data into a random part of memory
something to remember is DLL's run at a higher privilege level to normal programs, so some of the memory violations can cause crashes rather than showing errors

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Is there any reason why you're loading the library twice?
sudo rm -rf /