C++17编译DLL学习心得

C++17编译DLL学习心得

  1. 创建动态链接库
  2. 添加新建项C++文件
  3. 添加新建项H文件
  4. 编写代码
//gwdz.cpp代码
#include "gwdz.h"
#include "stdafx.h"
#include <urlmon.h>
#define MAX_SIZE 1024
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable: 4996)
#include<windows.h>
#include <atlstr.h>

//外网IP
//static char * WIP = new char[MAX_PATH];
extern "C" _declspec(dllexport) int GetInternetIP(char * &WIP)
{
	//	CoInitialize(NULL);
	char buf[MAX_PATH] = { 0 };    //把网页中读出的数据放在此处
	char chTempIp[128] = { 0 };
	char chIP[64] = { 0 };        //最终存放IP在此
								  //将网页数据写入c:\i.ini文件中

	CString pURL = "https://api.ip.sb/ip";
	CString pFileName = "C:\\a.html";
	//	DeleteUrlCacheEntry((LPCWSTR)pURL);
	if (URLDownloadToFile(0, pURL, pFileName, 0, NULL) == S_OK)
	{
		printf("URLDownloadToFile OK\n");
	}
	else
	{
		printf("URLDownloadToFile Fail,Error:%d\n", GetLastError());
	}
	FILE *fp = fopen("C:\\a.html", "r");
	if (fp != NULL)
	{
		fseek(fp, 0, SEEK_SET);
		fread(buf, 1, MAX_PATH, fp);
		fclose(fp);
	}

	//cout << strlen(buf) << endl;
	memcpy(WIP, buf, strlen(buf) - 1);
	WIP[strlen(buf) - 1] = 0;
	//	WIP = buf;

	printf("外网IP: %s\n", WIP);
	remove("C:\\a.html");
	//	CoUninitialize();
	return 0;
}

extern "C" _declspec(dllexport) int addtest(int a, int b) {
	return a + b;
}
//gwdz.h代码
#pragma once

extern "C" _declspec(dllexport) int addtest(int a, int b);
extern "C" _declspec(dllexport) int GetInternetIP(char * &WIP);

最后,编译成功,获得以下文件。
编译后文件

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值