C++17编译DLL学习心得
- 创建动态链接库
- 添加新建项C++文件
- 添加新建项H文件
- 编写代码
//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);
最后,编译成功,获得以下文件。