You*_*Yoo 2 c# c++-cli visual-c++
我的情况是这样的.
我有3个项目.
PrivacyDetectorDLL PDWrapper WindowsFormsApplication1(C#)
我的目标是在C#中使用Native C++类.所以我使用了C++/Cli包装类.但是我得到了保护级别的错误.我不明白.
我收到这个错误!
Error 1 'PDWrapper.PDWrapperClass.m_pCPrivacyDetectEngine' is inaccessible due to its protection level K:\Visual Studio 2010 Project\PrivacyDetecter\WindowsFormsApplication1\Form1.cs 23
Run Code Online (Sandbox Code Playgroud)
来自WindowsFormsApplication1项目的我的c#代码是这样的.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public PDWrapper.PDWrapperClass m_PDWrapper = null;
public Form1()
{
m_PDWrapper = new PDWrapper.PDWrapperClass();
unsafe
{
m_PDWrapper.m_pCPrivacyDetectEngine->initEngine();
}
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的PDWrapper类看起来像这样.
// PDWrapper.h
#pragma once
using namespace System;
namespace PDWrapper {
public ref class PDWrapperClass
{
// TODO: Add your methods for this class here.
public :
PDWrapperClass();
~PDWrapperClass();
public :
CPrivacyDetectEngine* m_pCPrivacyDetectEngine;
};
}
Run Code Online (Sandbox Code Playgroud)
这是我的PrivacyDetectEngine标题(来自PDWrapper项目的标题)
#pragma once
class CPrivacyDetectEngine
{
public: // my func
CPrivacyDetectEngine(void);
~CPrivacyDetectEngine(void);
void CPrivacyDetectEngine::initEngine();
void CPrivacyDetectEngine::startEngine(char* currentPath, size_t length);
public: // my util func
int CPrivacyDetectEngine::bytecmp(unsigned char *a, unsigned char *b, int length);
void CPrivacyDetectEngine::extToNum(char* fileName, unsigned int* extNum);
int CPrivacyDetectEngine::checkFileSig(unsigned char* fileSig, int sigLength, char* filePath, char** pFileInternal);
void CPrivacyDetectEngine::parseMSDocText(char** pFileInternal, unsigned int* compSize, unsigned int* decompSize);
char* CPrivacyDetectEngine::infData(char* pFileData, int compSize, int decompSize);
void CPrivacyDetectEngine::privacyDetectXML(char* text, int textLength, int* pItemIndex, char* filePath);
void CPrivacyDetectEngine::checkSocialNum(char* text);
public: // my var
int driverCount;
char driverName[256];
unsigned int parseFileList;
};
Run Code Online (Sandbox Code Playgroud)
来自PrivacyDetectorDLL的我的PrivacyDetectEngine标题是相同的,但在类CPrivacyDetectEngine之间有__declspec(dllexport)之类的
class __declspec(dllexport) CPrivacyDetectEngine
Run Code Online (Sandbox Code Playgroud)
我不知道有什么问题....有人请帮助我.
你的"包装"类没有完成它的工作.
C#无法访问本机C++类.这是错误的来源.指针是否存储在托管中ref class
并不重要.尽管指针是公共的,但它指向的数据类型对C#是隐藏的(因为它是不兼容的).
但是有一个解决方案.C++/CLI可以很好地访问本机C++类.所以你需要的是:
public ref class PDWrapperClass
{
// TODO: Add your methods for this class here.
public :
PDWrapperClass();
~PDWrapperClass();
private:
CPrivacyDetectEngine* m_pCPrivacyDetectEngine;
public:
void initEngine() { m_pCPrivacyDetectEngine->initEngine(); }
};
Run Code Online (Sandbox Code Playgroud)
您需要为希望C#能够调用的每个函数执行此操作.包装器不仅仅是一个指针架.在许多情况下,您可以在C++/CLI ref class
中提供一种调用整个本机函数序列的方法.
但是,请考虑使用智能指针,以确保在所有情况下都释放本机对象.例如,我在codereview.se上发布了一个智能指针.如果您使用它,请尊重许可; 这些条款非常慷慨,但确实强加了一些归属要求.