Thread: Help in exporting to a dll

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    5

    Help in exporting to a dll

    Hey all,
    I've exported a function to a dll and i think i got a problem with my typedef.

    this is the function in the dll:

    Code:
    int Refresh(FILE *f1,Item ListItem[])
    {
    ....
    }
    and this is my typedef:

    Code:
    typedef int  (CALLBACK *MYPROC2)(FILE*, Item*);
    does it look right to you? i'm not getting a value back when i try to call it.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are you sure you exported the function? Usually an exported function will have a prototype similar to:
    Code:
    __declspec(dllexport) int Refresh(FILE *f1,Item ListItem[])
    Although it may not if you use a .def file to specify the export.

    Also, how are you calling the function? Are you using LoadLibrary() and GetProcAddress()? If so, which of these is failing?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    i'll try to explain my self better.

    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 c file:

    Code:
    LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
         {
    ......
    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 (2,34); // Works!
    					FreeLibrary(hinstLib); 
    .......
    	hinstLib = LoadLibrary("MyProj.dll");
                  if (hinstLib != NULL) 
                    { 
                        ProcAdd2 = (MYPROC2) GetProcAddress(hinstLib, "Refresh");
                        if (fRunTimeLinkSuccess = (ProcAdd2 != NULL)) 
                        t=ProcAdd2 (poi,ListItem);
                        FreeLibrary(hinstLib); 
    
    }
    my def file:
    Code:
    EXPORTS
    adde
    Refresh
    The function "adde" works for me but when i try to use "Refresh" nothing comes into my "t" variable.
    Last edited by Shasoosh; 01-15-2010 at 04:15 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What do you mean nothing comes into your "t" variable? Do you mean the line:
    Code:
    t=ProcAdd2 (poi,ListItem);
    isn't being executed? If not, then that means GetProcAddress is failing. If you mean that ProcAdd2() is being executed, but nothing is being returned into "t", then you are mistaken.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    how can i know if GetProcAddress is failing?
    right now nothing is being returned into "t", i don't know what causing it but t stays 0 like i declared it.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Shasoosh View Post
    how can i know if GetProcAddress is failing?
    If it returns NULL, then it failed.


    Quote Originally Posted by Shasoosh View Post
    right now nothing is being returned into "t", i don't know what causing it but t stays 0 like i declared it.
    If your fread call in the Refresh function fails, then Refresh will return 0. In that case, t will stay at zero.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    ProcAdd2 not staying null for sure.
    i tried to do this at the beginning of the code:

    Code:
    	 int t=99;
    like you can see it stays that way..
    maybe this pic can help:

    http://dl.dropbox.com/u/17838/666re.PNG

    btw, the refresh function worked fine before i exported it to the dll so it must be something wrong i did on the way there.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In your screen shot, your code is at the ProcAdd2 line which means it hasn't called the function yet. "t" won't change value until after you call the function, so step the debugger to the next line.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    if i move the red point to the next line the debugger won't stop at all. any idea how to force it to stop?
    ps - if i add the line:
    Code:
    MessageBox(hwnd, "Test", "Test", MB_OK);
    after
    Code:
    t=ProcAdd2 (poi,ListItem)
    i won't see the pop. as if the code stops at the line
    Code:
    t=ProcAdd2 (poi,ListItem)
    Last edited by Shasoosh; 01-15-2010 at 07:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in exporting static function in win32 dll
    By dattaforit in forum Windows Programming
    Replies: 4
    Last Post: 12-04-2006, 12:03 PM
  2. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. Exporting Class methods to a DLL
    By EvBladeRunnervE in forum Windows Programming
    Replies: 2
    Last Post: 12-09-2003, 07:29 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM