C++&Pascal——用DEV C++/free pascal制作dll,并用C++/Pascal调用

来源:互联网 发布:单片机常用代码 编辑:程序博客网 时间:2024/06/05 11:55

dll代码:

pascal:

library DllNames;procedure ShowHelloWorld;stdcall;begin        assign(output,'DLL.out');        rewrite(output);        write('hello world');        close(output);end;exports        ShowHelloWorld;beginend.

c++:

ShowHelloWorld.cpp:

#include"dll.h"#include<cstdio>#include<windows.h>extern "C" _declspec(dllexport) void ShowHelloWorld(void);void ShowHelloWorld(void){freopen("Dll.out","w",stdout);printf("hello world");return;}

dll.h:

#ifndef _DLL_H_#define _DLL_H_#if BUILDING_DLL#define DLLIMPORT __declspec(dllexport)#else#define DLLIMPORT __declspec(dllimport)#endifclass DLLIMPORT DllClass{public:DllClass();virtual ~DllClass();//void HelloWorld();void ShowHelloWorld(void);};#endif


调用文件:

pascal:

procedure Fool;stdcall;external 'DllNames.dll' name 'ShowHelloWorld';procedure UsingFool;begin//      assign(output,'out.out');//      rewrite(output);//      write('dll fun !');//      close(output);        Fool;end;begin        UsingFool;end. 

c++:

#include<iostream>#include<cstdio>#include<Windows.h>#include<time.h>typedef void(*Dllfun)();using namespace std;int main(){/*freopen("out.out","w",stdout);printf("dll fun !");*/HINSTANCE hdll=LoadLibrary("dllnames.dll");if(hdll==NULL){FreeLibrary(hdll);}Dllfun ShowHelloWorld=(Dllfun)GetProcAddress(hdll,"ShowHelloWorld");if(ShowHelloWorld==NULL){FreeLibrary(hdll);}else{ShowHelloWorld();FreeLibrary(hdll);}return 0;}




原创粉丝点击