2013-05-13 29 views
2

我的情况是这样的。C#编译失败,因为“由于其保护级别”

我有3个项目。

PrivacyDetectorDLL PDWrapper WindowsFormsApplication1(C#)

我的目标是用本地C++类在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 

我从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(); 
     } 
    } 
} 

My PDWrapper Class看起来像这样。

// 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; 
    }; 
} 

,这是我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;  
}; 

从PrivacyDetectorDLL我PrivacyDetectEngine头是相同的,但确实有类CPrivacyDetectEngine之间__declspec(dllexport)的像

class __declspec(dllexport) CPrivacyDetectEngine 

我不知道是什么问题....有人请帮助我。

+0

您是否重建过所有的项目? – SLaks 2013-05-13 14:44:35

+0

将'class CPrivacyDetectEngine'标记为'public class CPrivacyDetectEngine'。 – Saravanan 2013-05-13 14:44:41

回答

5

你的“包装”类没有完成它的工作。

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(); } 
}; 

你需要为每个你希望C#能够调用的函数做到这一点。包装不仅仅是一个指针支架。在许多情况下,您可以在C++/CLI ref class中提供一种调用一系列本机函数的方法。

不过,请考虑使用智能指针,以确保本机对象在所有情况下都被释放。例如,我发布了a smart pointer on codereview.se。如果你使用它,请尊重许可证;这些条款非常慷慨,但确实会强加一些归因要求。

相关问题