To invoke a DLL written in C++ from C#

Asked 2 years ago, Updated 2 years ago, 40 views

Thank you for your help.

I'd like to call the DLL that my client gave me from my C# program.
The function specification is written in C++ and
Due to my lack of knowledge, I am having trouble knowing how to call from C#.

Thank you for your guidance.

// -- Specifications (in C++)

// functions:
longSample(FOO*foo);

// FOO structural member Var_1 (type: unsigned char, size 16, In)
Var_2 (type: unsigned char, size 24, In)
Var_3 (Type: unsigned char, Size 128, Out)

// -- Current Code --
// What I'm not sure about:
// Declaration and initialization part of the structure. Can C++ with unsigned char* be C# with Byte[N]...
// The part of the FromHexString function that converts hexadecimal strings to Byte.To be honest, I don't know how to pass the C# string.

class CSample
{
    // Call DLL SSampleFunc.dll を.For the argument, pass the structure FOO.
    DllImport("SampleFunc.dll", CallingConvention=CallingConvention.Cdecl)
    private external static long SampleFunc (FOO foo);

    // Define structure
    public structure FOO
    {
        public byte [ ] Var_1;
        public byte [ ] Var_2;
        public byte [ ] Var_3;
    }

    public FOO foo;

    public CSample()
    {
        // Initialization of structure
        foo.Var_1 = FromHexString("0C08"; // 4-character hexadecimal number
        foo.Var_2 = FromHexString ("123412341234123412341234123412341234"; //32 characters in every four digits
        foo.Var_3 = new byte [128]; // Buffer to retrieve string from DLL.
    }

    // Function to convert string to hexadecimal
    private static byte [ ] FromHexString(string str)
    {
        int length = str. Length/2;
        byte [ ] bytes = new byte [ length ];
        int j = 0;
        for (inti=0; i<length;i++)
        {
            bytes[i] = Convert.ToByte(str.Substring(j,2),16);
            j+=2;
        }
        return bytes;
    }

    // Function calling DLL
    private void ExecSample()
    {
        long ret = SampleFunc(foo); //ret contains an error code.
    }
}

c# c++

2022-09-30 17:47

1 Answers

Are Default marshalling or Marshaling data with platform calls helpful?(also the lower tier in the left tree of the page.)

// FOO structural member Var_1 (type: unsigned char, size 16, In)
Var_2 (type: unsigned char, size 24, In)
Var_3 (Type: unsigned char, Size 128, Out)

The corresponding C# code for is

 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public structure Foo{
    MarshallAs (UnmanagedType.ByValTStr, SizeConst=16) public string Var_1;
    MarshallAs (UnmanagedType.ByValTStr, SizeConst=24) public string Var_2;
    MarshallAs (UnmanagedType.ByValTStr, SizeConst=128) public string Var_3;
}

Also

// functions:
longSample(FOO*foo);

C# code corresponding to , but the size of long in the C++ language is environmentally dependent.For example, Visual C++, where long is 4 bytes.Therefore, you must select int which is also 4 bytes in the C# language (long in the C# language is 8 bytes).

If you declare it correctly as described above, no further action is required because the .NET Framework runtime translates correctly.

// The part of the FromHexString function that converts hexadecimal strings to Byte.To be honest, I don't know how to pass the C# string.

This point is unknown to third parties because the question statement does not indicate what values should be passed.


2022-09-30 17:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.