C生成Dll并调用


生成部分:




ToolDll.h

#pragma once


#ifndef WINVER                          // Specifies that the minimum required platform is Windows Vista.
#define WINVER 0x0600           // Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINNT            // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600     // Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINDOWS          // Specifies that the minimum required platform is Windows 98.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE                       // Specifies that the minimum required platform is Internet Explorer 7.0.
#define _WIN32_IE 0x0700        // Change this to the appropriate value to target other versions of IE.
#endif


#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>


#ifdef SOGOUPYTOOLDLL_EXPORTS
#define OUTPUT_API __declspec(dllexport)
#else
#define OUTPUT_API __declspec(dllimport)
#endif

//extern "C"  没有它的话,导出函数的名字是解析成C++模式的,使用不当会出问题
extern "C"  OUTPUT_API int nToolDll;

extern "C"  OUTPUT_API int fnToolDll(void);


ToolDll.cpp

#include "ToolDll.h"


// This is an example of an exported variable
OUTPUT_API int nToolDll=0;

// This is an example of an exported function.
OUTPUT_API int fnToolDll(void)
{
	return 42;
}



dllmain.cpp

// dllmain.cpp : Defines the entry point for the DLL application.
#include "ToolDll.h"

BOOL APIENTRY DllMain( HMODULE 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;
}




调用部分:

#include <iostream>
#include <Windows.h>
using namespace std;


typedef int (CallToolDll)(void);


int main()
{
	HINSTANCE hDllInst;
	
	hDllInst = LoadLibrary(L"ToolDll.dll");
	if (hDllInst == NULL)
	{
		cout <<"加载dll失败"<<endl;
	}
	else
	{
		CallToolDll * callFunc = (CallToolDll *)GetProcAddress(hDllInst,"fnToolDll");
		if (callFunc!=NULL)
		{
			cout << (*callFunc)() << endl;;
		} 
		 FreeLibrary(hDllInst);
	}
	return 0;
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值