More Group Sites
School Rankings
Jobless Net
Better Home
Enviro++


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest Search | Active Topics | Members | Log In | Register

Call unmanaged code with int* or char* using simple DllImport Options · View
hong
Posted: Saturday, November 14, 2009 7:37:51 AM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 329
Points: 693
Location: Australia
Call unmanaged code with int* or char* using simple DllImport

As discussed in How to call native C/C++ functions with pointers' parameters from C#, we can use Platform Invoke or DllImport to call native/unmanaged code with flat C API from a .NET application (eg. in C#).

P/Invoke can be very simple when only the isomorphic types are used as parameters in the exported functions of the native dll. The isomorphic types such as "int" in C# and C++ is identical. However, when the parameters of the function involve non-isomorhic types such as char* or int*, we need to do a bit more. For example, for char*, we must allocate char* buffer and receive string by pointer, we cannot use UnmanagedType.LPStr attribute, so we pass ANSI string as byte array. int* is more simple because it's 1-element Int32 array. See the following example:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("decrypt.dll", EntryPoint = "_GetSerialRegData"/*, ExactSpelling = false*/,SetLastError=true)]
        static extern void GetSerialRegData([MarshalAs(UnmanagedType.LPArray)] Int32[] Salt, [MarshalAs(UnmanagedType.LPArray)] byte[] SerialNo, [MarshalAs(UnmanagedType.LPArray)] byte[] ModId, [MarshalAs(UnmanagedType.LPArray)] Int32[] Days, [MarshalAs(UnmanagedType.LPArray)] Int32[] Qty, string SCode);

        public static void Main(string[] args)
        {
            byte[] SerialNo = new byte[6];
            byte[] ModId = new byte[6];
            Int32[] Salt = new Int32[1];
            Int32[] Days = new Int32[1];
            Int32[] Qty = new Int32[1];

            Salt[0] = 0;
            Days[0] = 0;
            Qty[0] = 0;

            string SCode = "BKXP8T3BXVRWUSAV77FDG";
            GetSerialRegData(Salt, SerialNo, ModId, Days, Qty, SCode);

            string strSerial = System.Text.Encoding.ASCII.GetString(SerialNo);
            string strModId = System.Text.Encoding.ASCII.GetString(ModId);
            int nSalt = Salt[0];
            int nDays = Days[0];
            int nQty = Qty[0];

        }
    }
}


In the example, I allocated 6 bytes for receiving each ANSI string, one element in Int32 array. For receiving string from byte array I used Text.Encoding.ASCII class.
Sponsor
Posted: Saturday, November 14, 2009 7:37:51 AM
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.367 seconds.