2010-11-19 6 views
1

안녕하세요. 내 관리되지 않는 c-dll에서 데이터를 다시 가져 오려고합니다. c 함수는 구조체에 대한 포인터를 기대하고, 구조체를 어떤 값으로 초기화하고 종료합니다. 실수는 C DLL을 선언 할 때조차도 어디에서나 발생할 수 있습니다.struct 포인터를 매개 변수로 사용하여 C 함수를 호출하십시오.

#ifndef MYFUNCS_H 
#define MYFUNCS_H 

__declspec(dllexport) typedef struct t_Point{ 
int x; 
int y; 
} Point; 

__declspec(dllexport) Point myFuncs(); 
__declspec(dllexport) int getPoint(Point* point); 
#endif 

C-파일 :

#include "stdafx.h" 
#include "OpenCVTest.h" 

int getPoint(Point* point){ 
point->x = 4; 
point->y = 2; 
return 0; 
} 

C#에서 래퍼 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 

namespace CSharp_mit_OpenCV 
{ 
    [StructLayout(LayoutKind.Sequential)] 
    public struct Point 
    { 
     public int x; 
     public int y; 
    }; 

    class Wrapper 
    { 
     [DllImport("OpenCV Test.dll", CharSet= CharSet.Auto)] 
     public static extern int getPoint(ref Point point); 

    } 
} 
다음

C 코드의 H-파일을 (나는이를 처음하고 있어요)

해당 래퍼를 사용하는 C# 함수 :

나는 다음과 같은 런타임 오류가이 코드

:

"의 PInvoke 함수에 대한 호출 'CSHARP의 MIT의 OpenCV의 CSharp_mit_OpenCV.Wrapper :: 참고 GetPoint가!'스택 불균형이있다. 관리되는 PInvoke 서명이 관리되지 않는 대상 서명과 일치하지 않기 때문일 수 있습니다. 이 cdecl을의 경우 호출 규칙과의 PInvoke 시그니처의 매개 변수는 목표 관리되지 않는 서명과 일치하는지 확인합니다. "

여기 무슨 일이야? 도와주세요! 은? 당신의 C 프로젝트에서 사용되는 calling convention

답변

0

! 여러분 모두 감사합니다 (기본 IIRC)를 사용하려면 DllImport 속성에 명시 적으로 지정해야합니다.

[DllImport("OpenCV Test.dll", CharSet = CharSet.Auto, 
    CallingConvention = CallingConvention.Cdecl)] 
public static extern int getPoint(ref Point point); 
관련 문제