2013-05-13 2 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(); 
     } 
    } 
} 

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

그리고 이것은 내 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 헤더가 동일하지만

class __declspec(dllexport) CPrivacyDetectEngine 
같은 클래스 CPrivacyDetectEngine 사이 __declspec (dllexport)를 가지고있다

나는 무엇이 문제인지 잘 모른다. ... 누군가 나를 도와주세요.

+0

모든 프로젝트를 재구성 했습니까? – SLaks

+0

'class CPrivacyDetectEngine'을'public class CPrivacyDetectEngine'으로 표시하십시오. – Saravanan

답변

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이라고 게시했습니다. 라이센스를 사용하는 경우 라이센스를 준수하십시오. 조건은 상당히 관대하지만 일부 귀속 요구 사항을 부과합니다.

관련 문제