// API mathAPI.h,在Dll.cpp和Test.cpp中
#ifdef __APIBUILD
#define __API __declspec(dllexport)
//#error __APIBUILD cannot be defined.
#else
#define __API __declspec(dllimport)
#endif
class math
{
public:
static __API double Pi;
static __API double Sum(double x, double y);
};
Run Code Online (Sandbox Code Playgroud)
//定义了Dll.cpp __APIBUILD
#include "mathAPI.h"
double math::Pi = 3.14;
double math::Sum(double x, double y)
{
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
// Test.cpp __APIBUILD未定义
#include <iostream>
#pragma comment(lib, "dll.lib")
#include "mathAPI.h"
int main()
{
std::cout << math::Pi; //linker error
std::cout << math::Sum(5.5, 5.5); //works fine
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的函数来获取进程'id的名字,但它总是返回0我尝试的每个进程:
DWORD GetProcessId(std::string ProcessName)
{
HANDLE hsnap;
PROCESSENTRY32 pt;
DWORD PiD;
hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pt.dwSize = sizeof(PROCESSENTRY32);
do {
if (!strcmp(pt.szExeFile, ProcessName.c_str())) {
CloseHandle(hsnap);
PiD = pt.th32ProcessID;
return PiD;
if (PiD != NULL) {
return 0;
}
}
} while (Process32Next(hsnap, &pt));
return 1;
}
Run Code Online (Sandbox Code Playgroud)
主要功能:
int main()
{
DWORD pid = GetProcessId("calc.exe");
std::cout << pid;
if (pid == 0) { printf("error 1"); getchar(); }//error
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是主要的"示例dlg.cpp"文件:
void CHelixV3Dlg::OnBnClickedCancel()
{
CEdit* editbox = (CEdit*)GetDlgItem(IDC_EDIT1);
//works fine, defined as: *CWnd GetDlgItem(int nID); in this file
}
Run Code Online (Sandbox Code Playgroud)
这是test.cpp源文件
void test()
{
CEdit* editbox = (CEdit*)GetDlgItem(IDC_EDIT1);
//does not work at all, seems to be a winAPI function instead of MFC...
//defined as: HWND __stdcall GetDlgItem(HWND hDlg, int nIDDlgItem);
}
Run Code Online (Sandbox Code Playgroud)
两个源文件都在同一个项目中,使用相同的头文件,但是test()的GetDlgItem显然是一个Win32 API函数,它在MFC中不起作用...我怎样才能在test.cpp中运行GetDlgItem()文件?
我试图调用函数testaddresscall(),其定义如下:
void testaddresscall()
{
printf("success");
}
int main(void)
{
void(*testaddresscallfunc)(void);
testaddresscallfunc= &testaddresscall;
cout << *testaddresscallfunc; //it printed 0x012D2050
typedef void testfunc(void);
testfunc* callbyaddress = (testfunc*)0x012D2050;
callbyaddress();
}
Run Code Online (Sandbox Code Playgroud)
然后发生这种情况
test.exe中0x012D2050处的未处理异常:0xC0000005:访问冲突执行位置0x012D2050.