Jared Tran

Jared Tran

  • NA
  • 1
  • 2k

Failed to call unmanaged functions in C++ dll file from C# application

Jul 10 2006 9:48 AM

My DriverFunctionTests.cpp has the following:

extern "C" __declspec(dllexport) void __stdcall InitDriver( unsigned short uChannel )

{

            pclDriver = new ClDriverWrap( uChannel );

}

 

My DataWrapper.cs has these:

[DllImport("DriverLibrary.dll")]

public static extern void InitDriver( ushort uChannel );

 

[DllImport("DriverLibrary.dll")]

public static extern void ResetDriver();

 

[DllImport("DriverLibrary.dll")]

public static extern bool IsDriverLoaded( ushort uChannel, [In(),arshalAs(UnmanagedType.LPArray)] byte [] strMessage );

 

And my DriverTester.cs has:

static void Main()

{

            try

            {

                        Application.Run(new Driver_Tester());

            }

            catch( Exception ex )

            {

                        MessageBox.Show( "Driver_Tester.Main(): " + ex.Message );

            }

}

private void Driver_Tester_Load(object sender, System.EventArgs e)

{

            ClDataWrapper.InitDriver(0);

}

 

To compile the whole solution in .NET, I put DriverFunctionTests.cpp in the project DriverLibrary, and I put DataWrapper.cs and DriverTester.cs in anther project DriverTester. After compiling, I copy and paste the DriverLibrary.dll  to the Debug folder of the project DriverTester.

 

If I compile this solution in .NET, the code works great. However, if I compile DriverFunctionTests.cpp in Visual Studio 6.0, copy and paste the dll file, then the C# code throws the exception saying "Unable to load the DriverLibrary.dll".

 

Appreciate your help.