How can I call C function in VC#2005? Please do me a favour, for example

I will call MyLogin() in my C#, how to deal with LPTSTR, pointer, and Struct. TOKEN_CONTEXT is a struct which was pass to MyLogin(). Thanks.

---------------------------------------------------------------------------------------------------------------------------

unsigned int __declspec( dllexport ) MyLogin ( unsigned long int *pSlotID,

LPTSTR pName, // file name or for boot code: the data

unsigned char * pData, // The data (if applicable)

int dataLen, // dataLen (if applicable)

unsigned long int userType,

LPTSTR pPin,

unsigned long int ulPinLen,

unsigned long int sessionFlags, // we may need RW_SESSION flag here..

unsigned long int * pSession,

TOKEN_CONTEXT * pTokenContext) //struct

--------------------------------------------------------------------------------------------------------------------------

typedef struct TOKEN_CONTEXT {

int phase;

unsigned long int hSession;

HINSTANCE hDLL;

unsigned long int slotID;

CK_FUNCTION_LIST_PTR pFunc; // function list obtained from DLL (pointing to what we are using now)

unsigned char DLLName[MAX_PATH];

unsigned char DLLPath[MAX_PATH];

unsigned char TokenName[MAX_PATH];

CK_SLOT_INFO SlotInfo;// struct

unsigned long int hSessionCallback;

unsigned char hashedPIN[HASH_SIZE];

SC_CONFIG_STRU ConfigStru; //struct

} TOKEN_CONTEXT;

-----------------------------------------------------------------------------------------------------------------

the following is my code in my C# program, I didn't finish it, I didn't know how to continue.

[DllImport("mfcdll.dll", EntryPoint = "MyLogin", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl, SetLastError=true)]

static extern int MyLogin ( CK_SLOT_ID *pSlotID,

LPTSTR pName,

byte pData,

int dataLen,

UInt32 userType,

LPTSTR pPin,

UInt32 ulPinLen,

UInt32 sessionFlags,

UInt32 pSession,

TOKEN_CONTEXT * pTokenContext);




Answer this question

How can I call C function in VC#2005? Please do me a favour, for example

  • Sergio Ordine

    You want to use a C++/CLI dll here, and reference the dll in your C# application.

    C++/CLI allows you to mix native and managed code.

    Here is an example, using MessageBeep()

    C++/CLI code, mixed native and managed

    #include "windows.h"< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    using namespace System;

    using namespace System::Runtime::InteropServices;

    public ref struct Beep

    {

        enum struct BeepTypes : unsigned int

        {

            Beep        = (unsigned int) -1,

            Asterix     = MB_ICONASTERISK,

            Exclamation = MB_ICONEXCLAMATION,

            Hand        = MB_ICONHAND,

            Question    = MB_ICONQUESTION,

            OK          = MB_OK,

        };

        static void MessageBeep(enum class BeepTypes beep)

        {

             ::MessageBeep((unsigned int)beep);

        }

    };

    void main()

    {

        Beep::MessageBeep(Beep::BeepTypes::Exclamation);

    }

    We can compile and run this as follows:

    C:\>cl /nologo /clr beep.cpp user32.lib

    C:\>beep

    And here is the C# code that calls it:

    class Test

    {

        public static void < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main()

        {

            Beep.MessageBeep(Beep.BeepTypes.Exclamation);

        }

    }

    C:\>cl /nologo /LD /clr beep.cpp user32.lib

    C:\>csc /nologo /r:beep.dll test.cs

    C:\>test

    Hope that helps...

  • How can I call C function in VC#2005? Please do me a favour, for example