我用VC调用dll时,遇到的几种方法都是在头文件中包含dll的.h文件或导入.tlb文件,请问有没有什么方法在不包含dll的.h或.tlb文件的情况下调用dll的方法?

解决方案 »

  1.   

    // File:  RUNTIME.C
    // A simple program that uses LoadLibrary and 
    // GetProcAddress to access myPuts from MYPUTS.DLL. 
     
    #include <stdio.h> 
    #include <windows.h> 
     
    typedef VOID (*MYPROC)(LPTSTR); 
     
    VOID main(VOID) 

        HINSTANCE hinstLib; 
        MYPROC ProcAdd; 
        BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
     
        // Get a handle to the DLL module.
     
        hinstLib = LoadLibrary("myputs"); 
     
        // If the handle is valid, try to get the function address.
     
        if (hinstLib != NULL) 
        { 
            ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts"); 
     
            // If the function address is valid, call the function.
     
            if (fRunTimeLinkSuccess = (ProcAdd != NULL)) 
                (ProcAdd) ("message via DLL function\n"); 
     
            // Free the DLL module.
     
            fFreeResult = FreeLibrary(hinstLib); 
        } 
     
        // If unable to call the DLL function, use an alternative.
     
        if (! fRunTimeLinkSuccess) 
            printf("message via alternative method\n"); 

      

  2.   

    下面是VC生成的程序
    #include "stdafx.h"
    #include "dll1.h"BOOL APIENTRY DllMain( HANDLE hModule,
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
     )
    {
        switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
    break;
        }
        return TRUE;
    }
    // This is an example of an exported variable
    DLL1_API int nDll1=1000;// This is an example of an exported function.
    DLL1_API int MyPut(void)
    {
    return 42;
    }// This is the constructor of a class that has been exported.
    // see dll1.h for the class definition
    CDll1::CDll1()

    return; 
    }我这样调用的时候:
    void CTestdllDlg::OnOK() 
    {
    typedef VOID (*MYPROC)(LPTSTR); 
    // TODO: Add extra validation here
    MYPROC ProcAdd;
    char * FunName = "MyPut";
    HINSTANCE  dll_okno = LoadLibrary("dll1.dll");
    if( dll_okno == NULL)
    MessageBox("Load Dll Fail","Waring",MB_OK);
    else
    {
    ProcAdd = (MYPROC) GetProcAddress(dll_okno, FunName);
    if( ProcAdd != NULL)
    {
    MessageBox("OK", "Waring", MB_OK);
    (ProcAdd);
    }
    else
    MessageBox("No FUNCTION", MB_OK);
    FreeLibrary(dll_okno);
    }
    }
    为什么总是说找不到这个MyPut函数??
      

  3.   

    楼上的这可能是字符格式的问题,2000下大部分函数参数中的字符串都是Unicode你应该这样
    ProcAdd = (MYPROC) GetProcAddress(dll_okno, "MyPut");
    或者
    TCHAR * FunName = "MyPut";
      

  4.   

    try dumpbin to check whether the function "MyPut" is really exported or not.
      

  5.   

    ?MyPut@@YAHXZ
    是这个吗?我在exp文件中找到的