首先,我是 C++ 新手(近一周),如果这很明显,请原谅我。另外,我搜索了许多具有类似问题的帖子。要么我的理解不够深入,要么没有相关信息可以帮助我理解这个问题。
在 Metatrader 4 中,我试图弄清楚如何将结构变量传递给 dll,并修改存储在所述结构中的变量。到目前为止,我已经取得了巨大的成功,即使在处理结构数组时也是如此。然后我遇到了一个问题。
我已将问题缩小到使用字符串。如果您愿意,请查看以下代码,我曾用它来专注于解决此问题,并帮助我理解为什么每当我尝试在 mt4 中运行脚本时,我都会不断收到此“访问冲突写入 0x00000000”错误.
mql4 代码:
struct Naming
{
string word;
} name;
#import "SampleDLLtest.dll"
bool NameTest(Naming &name);
#import
int init() { return(0); }
int start()
{
Print("original name: ", name.word);
if( NameTest( name ) )
{
Print("new name: ", name.word);
}
//---
return(0);
}
这是相关的dll代码:
#define WIN32_LEAN_AND_MEAN
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
BOOL APIENTRY DllMain(HANDLE 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);
}
struct Naming
{
std::string n_name;
};
bool __stdcall NameTest(Naming *name)
{
name->n_name = "Captain Success";
return true;
}