1

我正在尝试使用我在 c# 中用 c++ 构建的 dll 文件中的函数;下面是 c++ .h 代码

#pragma once
#pragma unmanaged

#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
#include <list>
#include <string>
using namespace std;

extern "C"
{
string DLLIMPORT encrypt(string keeper);
string DLLIMPORT decrypt(string keeper);
}

#endif /* _DLL_H_ */

c++ .cpp 代码如下

#pragma managed
#include "cenH.h"
#include <windows.h>
#include <iostream>
using namespace std;

const char alphabets[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z'};

string encrypt(string keeper)
{
string keep;

string m = keeper;
cout << keeper.length() << endl;
for(int count = 0; count < m.length(); count++)
{
   for(int c = 0; c < 26; c++)
   {
        if(m[count] == alphabets[c])
        {

                    int counter = c - 8;
                    if(counter < 0)
                    {
                               counter = counter + 26; 
                               keep += alphabets[counter];

                    }
                    else
                    {
                               keep += alphabets[counter];      
                    }
        }    
        else if(m[count] == ' ')
        {
             keep+=".";
             break;
        }

   }//end of second loop

//end first loop
cout << keep << endl;


 return keep;
}

string decrypt(string keeper)
{ 
 //cout << "\n" << endl;   
 string keep;

      string m = keeper;
      for(int count = 0; count < m.length(); count++)
      {
           for(int c = 0; c < 26; c++)
           {
                if(m[count] == alphabets[c])
                {
                            int counter = c + 8;
                            if(counter >= 26)
                            {
                                       counter = counter - 26; 
                                       keep += alphabets[counter];                                                  
                            }
                            else
                            {
                                       keep += alphabets[counter];

                            }
                }else if(m[count] == '.')
                {
                      keep+=" ";
                      break;
                }   
           }//end of third loop

      }//end second loop
        //cout << keep << endl;

 return keep;
}


BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                   DWORD reason        /* Reason this function is being called. */ ,
                   LPVOID reserved     /* Not used. */ )
{
  switch (reason)
  {
  case DLL_PROCESS_ATTACH:
    break;

  case DLL_PROCESS_DETACH:
    break;

  case DLL_THREAD_ATTACH:
    break;

  case DLL_THREAD_DETACH:
    break;
}

/* Returns TRUE on success, FALSE on failure */
return TRUE;
}

我编译了这些代码,它们没有给出错误,我什至通过调用 dll 文件在 c++ 控制台应用程序中测试运行它们,它工作正常,但是当我将此 dll 文件调用到我的 c# 程序中时,它给了我 AccessViolationException Attempted to read or write受保护的内存。这通常表明其他内存已损坏。dll 在 MinGW GCC 4.6.2 32 位中编译,我的 c# 在 x86(32 位)上运行。有人可以指出我做错了什么。

这就是我在我的 c# 代码中导入 dll 的方式

[DllImport("cen.dll",CallingConvention = CallingConvention.StdCall)]
internal static extern string decrypt(String keeper);
string line = decrypt("savethemonkeys");
4

0 回答 0