我有 2 个 DLL。一个是 PrimaryDLL.dll,另一个是 DLLWrap.dll。PrimaryDLL 有一个对象,一个名为 DiversifyKeyset 的函数,它在类构造函数中像这样贴花:
static PRIMARYDLL_API std::string DiversifyKeyset(std::string Key1, std::string Key2, std::string Key3);
这是定义的方法:
    std::string tKeyset::DiversifyKeyset(std::string Key1, std::string Key2, std::string Key3)
{
    tKeyset* keyset = new tKeyset();
    keyset ->Key1 = "12345678";
    keyset ->Key2 = "23456789";
    keyset ->Key3 = "34567890";
    //return keyset --eventually I want to return this
    return 0;
}
我的 DLLWrap.dll 在它的 .cpp 文件中调用这个函数:
cout << "Here is what is returned:  " <<
firstDllLayer::tKeyset::DiversifyKeyset(a,b,c) << endl;
现在这一切都编译好了,库也建好了。当我调用 C# 代码调用第二个 DLLWrap.dll 时,我的错误出现了。我收到错误:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
所以我一直在绞尽脑汁想为什么这不起作用。我承受着很大的压力,但似乎找不到我收到此内存写入错误的原因。
所以为了帮助我提供我的代码。
我的 C++ PrimaryDLL.h 文件:
#pragma once
#include "stdafx.h"
#include <string>
#include <stdexcept>
extern "C"{
#ifdef PRIMARYDLL_EXPORT
    #define PRIMARYDLL_API __declspec(dllexport) 
#else
    #define PRIMARYDLL_API __declspec(dllimport)  
#endif
namespace firstDllLayer
{
class tKeyset
{
public:
    tKeyset();
    std::string Key1;
    std::string Key2;
    std::string Key3;
    tKeyset(std::string _key1, std::string _key2, std::string _key3);
    static PRIMARYDLL_API std::string DiversifyKeyset(std::string Key1, std::string Key2, std::string Key3);
};
tKeyset::tKeyset()
{
    Key1 = "";
    Key2 = "";
    Key3 = "";
}
tKeyset::tKeyset(std::string _Key1, std::string _Key2, std::string _Key3)
{
    Key1 = _Key1;
    Key2 = _Key2;
    Key3 = _Key3;
}
}
}
这是 PrimaryDll.cpp 文件:
// PrimaryDll.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "PrimaryDll.h"
#include <stdexcept>
#include <string>
using namespace std;
namespace firstDllLayer
{
    tKeyset* MasterKeyset = new tKeyset("1234","5678","0910");
    std::string tKeyset::DiversifyKeyset(std::string Key1, std::string Key2, std::string Key3)
{
    tKeyset* keyset = new tKeyset();
    keyset ->Key1 = "12345678";
    keyset ->Key2 = "23456789";
    keyset ->Key3 = "34567890";
    //return Keyset; -eventually I would like to return this
    return 0;
}
}
这是我的 DLLWrap.h 文件:
#pragma once
#include "stdafx.h"
#include "Primary.h"
#include <stdexcept>
#include <string>
extern "C"{
#ifdef DLLWRAPDLL_EXPORT
    #define DLLWRAP_API __declspec(dllexport) 
#else
    #define DLLWRAP_API __declspec(dllimport)  
#endif
namespace MyFunc
{
    class MyCall
        {
        public:
            static DLLWRAP_API std::string DiversifyKeysetCall(std::string a,std::string b,std::string c);
    };
}
}
DLLWrap.cpp 文件:
// DLLWrap.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "PrimaryDll.h"
#include <stdexcept>
#include "DLLWrap.h"
#include <iostream>
#include <string>
using namespace std;
namespace MyFunc
{
    std::string MyCall::DiversifyKeysetCall(std::string a,std::string b,std::string c)
    {
            /*cout << "Here is what is returned:  " <<
                firstDllLayer::tKeyset::DiversifyKeyset(a,b,c) << endl;
            return 0;*/
        cout << "Here is what is returned:  " <<
                firstDllLayer::tKeyset::DiversifyKeyset(a,b,c) << endl;
            return 0;           
    }
}
最后是 C# Program.cs 文件:
// Marshal.cs
using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest
{    public class DllHelper
    {
        [DllImport(@"C:\Users\user\Documents\Visual Studio 2010\Projects\KeyDll\Debug\DLLWrap.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?DiversifyKeysetCall@MyCall@MyFunc@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V34@00@Z")]
        public static extern string DiversifyKeysetCall(string a, string b, string c);
    }
    static void Main()
    {    
        try
        {
            DllHelper.DiversifyKeysetCall("1234","5678","0910");           
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        }
}
在我的 PrimaryDLL.cpp 文件中,我最终想返回该对象,但因为它在如何引用指针以及何时离开时非常混乱:
return keyset; 
未评论我收到无法从对象类型转换为字符串类型的错误。
因此,请任何帮助将不胜感激为什么我会收到“尝试读取或写入受保护的内存。这通常表明其他内存已损坏”错误。
此致。