My situation is this.
I have 3 Projects.
PrivacyDetectorDLL PDWrapper WindowsFormsApplication1 (C#)
My goal is to use Native C++ class in C#. So I used C++/Cli wrapper class. But I get error of protection level. I don't get it.
I get this error!
Error 1 'PDWrapper.PDWrapperClass.m_pCPrivacyDetectEngine' is inaccessible due to its protection level K:\Visual Studio 2010 Project\PrivacyDetecter\WindowsFormsApplication1\Form1.cs 23
My c# code from WindowsFormsApplication1 project is this.
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();
}
}
}
My PDWrapper Class looks like this.
// 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;
};
}
And This is my PrivacyDetectEngine header ( header from PDWrapper Project )
#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;
};
My PrivacyDetectEngine header from PrivacyDetectorDLL is same but does have __declspec(dllexport) between class CPrivacyDetectEngine like
class __declspec(dllexport) CPrivacyDetectEngine
I have no idea what is the problem.... Someone please help me.
Your "wrapper" class isn't doing its job.
C# can't access native C++ classes. This is the source of the error. It doesn't matter if the pointer is stored in a managed ref class
or not. Although the pointer is public, the data type it points to is hidden from C# (because it is incompatible).
There is a solution, though. C++/CLI can access native C++ classes just fine. So what you need is:
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(); }
};
You need to do this for each function you want C# to be able to call. A wrapper is more than just a pointer holder. In many cases you can provide one method in the C++/CLI ref class
that calls a whole sequence of native functions.
Do consider using a smart pointer, though, to make sure the native object is freed in all cases. For example, I posted a smart pointer on codereview.se . Please do respect the license if you use it; the terms are quite generous but do impose some attribution requirements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.