2012-12-11 2 views
4

C# 응용 프로그램에서 c dll을 사용하는 데 약간의 문제가 있습니다. 나에게 오류를 제공 기능은이 같은 DLL의 헤더 파일에 정의되어 C에서 DLL을 C DLL로 전달 #

int __stdcall DDC_CreateFilePropertyString (DDCFileHandle file, 
             const char *property, 
             const char *value); 

은 내가 DLL을 액세스 내 클래스에 다음 코드를 추가했다.

[DllImport("nilibddc.dll", CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Ansi)] 
private static extern int DDC_CreateFilePropertyString(IntPtr file, 
         [MarshalAs(UnmanagedType.LPStr)]string property, 
         [MarshalAs(UnmanagedType.LPStr)]string value); 

유형 DDCFileHandle은 다음과 같이 헤더 파일에 정의되어 있습니다

typedef struct _DDCFile DDCFile; 
typedef DDCFile* DDCFileHandle; 

난에서 다른 파일이없는 (헤더 파일에서 _DDCFile 구조체에 대한 추가 정보가 없습니다 라이브러리 메신저 사용).

DDC_CreateFilePropertyString() 함수를 호출하기 전에 다음 함수를 호출하여 파일을 만들고 파일 핸들을 가져옵니다.

[DllImport("nilibddc.dll", CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Ansi] 
private static extern int DDC_CreateFile(char[] filePath, 
          char[] fileType, 
          char[] name, 
          char[] description, 
          char[] title, 
          char[] author, 
          ref IntPtr file); 

헤더 파일의 정의는 다음과 같습니다. 내가 DDC_CreateFilePropertyString 함수를 호출 할 때

int __stdcall DDC_CreateFile (const char *filePath, 
          const char *fileType, 
          const char *name, 
          const char *description, 
          const char *title, 
          const char *author, 
          DDCFileHandle *file); 

지금 항상 내가 전달 일부 잘못된 매개 변수가 있음을 말해 나에게 오류를 반환합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 내가 사용하고있는 라이브러리는 National Instruments의 TDMS C API입니다.

도움 주셔서 감사합니다.

+1

'DDC_CreateFile '에 대한 호출이 실제로 성공합니까? 어떻게 알았어? 그리고 그것의'const char *'매개 변수도'char []'보다는'string' ('DDC_CreateFilePropertyHandle'와 동일)로서 전달되어야합니까? DLL 함수도'stdcall'으로 선언되므로 pinvoke 선언에서'cdecl' 호출 규칙을 변경해야합니다. – shambulator

+0

예. 성공합니다. 반환 코드는 괜찮습니다. 방금 'DDC_CreateFile'함수를 호출하면 파일 시스템에 파일이 제대로 작성됩니다. 'char []'을 'string'으로 변경하고 'cdec1'을 'stdcall'으로 변경했지만 여전히 오류가 발생합니다. – Devkev

답변

2

p/invokes이 약간 있습니다. 기본값 인 CallingConvention.Stdcall을 사용해야합니다. 그리고 const char* 매개 변수의 경우 C# 끝에 string이되도록 간단히 선언해야합니다.

[DllImport("nilibddc.dll", CharSet=CharSet.Ansi] 
private static extern int DDC_CreateFile(
    string filePath, 
    string fileType, 
    string name, 
    string description, 
    string title, 
    string author, 
    ref IntPtr file 
); 

그리고 DDC_CreateFilePropertyString 당신이 필요합니다 :

올바른 C#을 p는/DDC_CreateFile에 대한 호출은이 함수를 호출 할 때

[DllImport("nilibddc.dll", CharSet=CharSet.Ansi)] 
private static extern int DDC_CreateFilePropertyString(
    IntPtr file, 
    string property, 
    string value 
); 

경우, 코드를 수정 한 후, 당신은 여전히 ​​오류가 발생을 , 라이브러리를 잘못 사용하고있는 것입니다. 그리고 그것은이 질문의 범위를 벗어납니다. 설명서를 참조하거나 도서관 공급 업체의 지원을 요청하십시오.

+0

도움을 주셔서 감사합니다. 시도했지만 작동하지 않았습니다. 공급 업체에 연락해야합니다. 감사! – Devkev

관련 문제