我写了个DLL文件,其中有个函数是
STDMETHODIMP MYServer::StartServer(int *RetVal)
{
    if (SHAREDServer.StartServer())
*RetVal = 1;
else
*RetVal = 0;
return S_OK;
}
不知道如何在其他的工程中调用,最好有源代码,LoadLibrary和FreeLibrary
的操作我会,主要是中间的获得指针的函数。

解决方案 »

  1.   

    因为你使用了类,最好是将整个类输出,否则别的程序不好调用,因为member function的参数里面有一个隐含的this指针。要输出类使用AFX_EXT_CLASS宏。
      

  2.   

    我不能修改源程序和DLL文件
      

  3.   

    一个例子,供参考
    //testDll.cpp
    #include "stdio.h"
    #include "windows.h"
    //#include "testDll.h"
    void main()
    {
    //本来定义的格式为:typedef int (* pMax)(int a,int b);
    //编译没有问题,但是执行总会出错,改成下面的形式,问题解决
    typedef int (__stdcall * pMax)(int,int);
    typedef int (__stdcall * pMin)(int,int);
    HINSTANCE hLibrary;
    hLibrary=LoadLibrary("dlltest.dll");
    pMax nMax;
    nMax=(pMax)GetProcAddress(hLibrary,"nMax");
    int max=nMax(66,130);
    printf("%d\n",max);
    FreeLibrary(hLibrary);
    }下面是DLL的source code
    // dlltest.cpp : Defines the entry point for the DLL application.
    //
    #include "dlltest.h"
    #include "stdafx.h"BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
     )
    {
    switch( ul_reason_for_call )
    {
    case DLL_PROCESS_ATTACH:
    break;
    case DLL_THREAD_ATTACH:
    break;
    case DLL_THREAD_DETACH:
    break;
    case DLL_PROCESS_DETACH:
    break;
    }
    return TRUE;}
    int WINAPI nMax(int a, int b)
    {
    if(a>=b)return a;
    else
    return b;
    }
    int WINAPI nMin(int a, int b)
    {
    if(a>=b)return b;
    else
    return a;
    }
      

  4.   

    通过AFX_EXT_CLASS  进行声明调用
    然后再使用EXTERN‘C’ 来用纯C做