Unmanaged / managed interop - problem with passing int []

I am working on my chemical composition and for this reason I need to write a software application to help me display samples under a microscope. This microscope is equipped with xyz nanostructure overlay. The stage is managed using an unmanaged DLL written in VC ++ by the hardware vendor. I could provide you with more details, but let me start with this:

One of the methods in the dll allows me to read the settings for the axis of movement:

C ++ Syntax:

BOOL E7XX_qSVO (int ID, const char* szAxes, BOOL* pbValueArray) 

Where BOOL is int 0 or 1 as agreed.

My C # wrapper contains:

[DllImport("E7XX_GCS_DLL.dll", EntryPoint = "E7XX_qSVO")]   
public static extern int qSVO(int iId, string sAxes, int []iValArray);

That seems right to me. However, when I try to do something like this in my main application (for requesting axes 1,2 and 3):

Int32 [] _iValues = new Int32[3];
E7XXController.qSVO(m_iControllerID, "123", _iValues);

I sequentially get this array:

{6, 0, 10}, {0, 0, 0} . :

BOOL E7XX_SVO (int ID, const char* szAxes, const BOOL* pbValueArray) ...

dll . , BOOL...

, , , ?

+3
4

BOOL ++ "int", , System.Int32, System.Boolean. , COM- , VARIANT_BOOL, System.Boolean. , ?

+1

MarshalAs, : -

[DllImport("E7XX_GCS_DLL.dll", EntryPoint = "E7XX_qSVO")]   
public static extern int qSVO(int iId, 
                              [MarshalAs(UnmanagedType.AnsiBStr)]string sAxes, 
                              [MarshalAs(UnmanagedType.LPArray)]int[] iValArray);

, sAxes char, : -

[DllImport("E7XX_GCS_DLL.dll", EntryPoint = "E7XX_qSVO")]   
public static extern int qSVO(int iId, 
                              [MarshalAs(UnmanagedType.LPArray)]char[] sAxes, 
                              [MarshalAs(UnmanagedType.LPArray)]int[] iValArray);

, interop , , , UnmanagedType, . , , , !

+1

, Interop Win32 Pinvoke.net. Visual Studio.

http://www.pinvoke.net/index.aspx

0

:

V++ "C" extern. , Win-API. , V++ WINAPI __stdcall .

The default calling convention for DllImportis WinApi . On Windows, this is StdCall. But according to CE, this is Cdecl. You want to make sure that you are using the correct calling convention. You can add a tray:

CallConvention = CallingConvention.StdCall

Also indicate our character set:

CharSet = CharSet.Ansi

But he should work even without them. This is pretty weird as your code looks right.

0
source

Source: https://habr.com/ru/post/1702522/


All Articles