dll_入門2,隱士利用dll匯出的類,變數,有參函數

來源:互聯網
上載者:User

trydll2.h

// from a DLL simpler. All files within this DLL are compiled with the TRYDLL2_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// TRYDLL2_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef TRYDLL2_EXPORTS
#define TRYDLL2_API __declspec(dllexport)
#else
#define TRYDLL2_API __declspec(dllimport)
#endif

// This class is exported from the trydll2.dll
class TRYDLL2_API CTrydll2 {
public:
 CTrydll2(void);
 void diplay();
 int fnp(int i);
 // TODO: add your methods here.
};

extern TRYDLL2_API int nTrydll2;

extern "C"
{
TRYDLL2_API CTrydll2 * GetInstance();
TRYDLL2_API int fnTrydll2(void);
TRYDLL2_API int fnp(int para);
};

//TRYDLL2_API int fnTrydll2(void);//在c++檔案中使用會出錯
//c++編譯器對函數進行改名 修改後的名字依賴於 未修改的函數名,函數的參數類型,函數的參數個數
//函數所在的類(如果有),函數所在的檔案。
//我在用隱士方法利用dll時,由於dll中的函數(設為void fn() )沒有聲明為extern "C",所以按照c++規則修改
//然後再用戶端(dll為伺服器)直接用函數fn(),編譯能夠進行但是在運行時出錯,
//系統提示:無法找到函數的進入點。因為用戶端的fn也進行了改名,結果修改後的名字和伺服器修改後的名字不一樣
//所以 我才猜測 c++對函數名的修改也依賴於檔案名稱..
//事實我的猜測是錯誤的,但是記住,不要把希望寄託在 系統改名會讓連個函數名字一樣 

、、。trydll2.cpp。。。。。。。。。。

#include "stdafx.h"
#include "trydll2.h"
//..............................
#include <iostream>
using namespace std;
///...............................
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
TRYDLL2_API int nTrydll2=0;

// This is an example of an exported function.
TRYDLL2_API int fnTrydll2(void)
{
 cout<<"fnTrydll2 /n";
 return 42;
}

TRYDLL2_API CTrydll2 * GetInstance()
{
 return new CTrydll2;
}
TRYDLL2_API int fnp(int para)
{
cout<<"fnp:"<<para<<endl;
return para=1;
}
// This is the constructor of a class that has been exported.
// see trydll2.h for the class definition
CTrydll2::CTrydll2()
{
 return;
}

void CTrydll2::diplay()
{
 cout<<"CTrydll2::diplay"<<endl;
}

int CTrydll2::fnp(int i)
{
 cout<<"CTrydll2::fnp:" <<i<<endl;
 i++;
 return i;
}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、

/*隱士使用dll匯出的 類 ,變數,無參函數,有參函數,與一般的使用方法一樣!!!!!!

*/
#include "stdafx.h"

/*
隱士調用的方法:假如標頭檔 eg:#include "../trydll2.h"
假如產生dll時產生的lib檔案。
直接調用函數。eg:fnTrydll2();
*/
#include "../trydll2.h"
#include "windows.h"
#include <iostream>
using namespace  std;

int main(int argc, char* argv[])
{
 cout<<"used dll with hide way:";
 fnTrydll2();
 //fnTrydll2:別忘了C++預設編譯器會對函數進行改名
 //所以你應該強制什麼聲明不應該改名
 //但是這樣的話 重載就有麻煩了。。因為重載就是應用了改名的方法
 fnp(200);//有參函數
 //..............
 nTrydll2 = 20;//ok
 cout<<"ntrydll2:"<<nTrydll2<<endl;
 
 CTrydll2 ct;
 ct.diplay();
 ct.fnp(10 );
 CTrydll2 *pct = GetInstance();
 pct->diplay();
 /*
 隱士引用dll的匯出變數和 類 很簡單,和一般的用法沒什麼區別
 */
 return 0;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.