2010-12-08 5 views
2

저는 C# 구현 측면에 갇혀 있습니다. 문제는, 내 C++ 응용 프로그램이 pchListSoftwares 버퍼를 pchInstalledSoftwares에 복사 할 수 있도록 C# 코드에서 '포인터'(메모리가 있음)를 전달하려고합니다. 포인터를 C# 측에서 전달하는 방법을 알아낼 수 없습니다.C#에서 char 포인터를 C++ 함수로 전달

네이티브 C++ 코드 (MyNativeC++ DLL.dll)

void GetInstalledSoftwares(char* pchInstalledSoftwares){ 
    char* pchListSoftwares = NULL; 
    ..... 
    ..... 
    pchListSoftwares = (char*) malloc(255); 

    /* code to fill pchListSoftwares buffer*/ 

    memcpy(pchInstalledSoftwares, pchListSoftwares, 255); 

    free(pchListSoftwares); 

} 

간단한 '문자열을'전달 작동하지 않습니다 ...

C#을 구현

[DllImport("MyNativeC++DLL.dll")] 
private static extern int GetInstalledSoftwares(string pchInstalledSoftwares); 


static void Main(string[] args) 
{ 
......... 
......... 
     string b = ""; 
     GetInstalledSoftwares(0, b); 
     MessageBox.Show(b.ToString()); 
} 

도움의 모든 종류의 대단히 감사합니다 ...

답변

2

StringBuilder 내에게

[DllImport("MyNativeC++DLL.dll")] 
private static extern int GetInstalledSoftwares(StringBuilder pchInstalledSoftwares); 


static void Main(string[] args) 
{ 
......... 
......... 
     StringBuilder b = new StringBuilder(255); 
     GetInstalledSoftwares(0, b); 
     MessageBox.Show(b.ToString()); 
} 
1

내 실수 ... 0을 (를) 호출하여 GetInstalledSoftwares(0, b);에 전화를 겁니다. .

private static extern int GetInstalledSoftwares(ref string pchInstalledSoftwares); 

이 (참조로 문자열을 보내기) :

+0

좋은 직장을 사용해보십시오 (그것을 알아내는) –

0

봅니다 프로토 타입 라인을 변경합니다.

관련 문제