Discussion:
Problem call unmanaged dll from C#
(too old to reply)
Marc Oden
2010-02-03 17:40:00 UTC
Permalink
I have written a unmanaged C++ dl that I am attempting to call from a C#
app. The dll (HitVeinWin32.dll) is in the same directory as the C# apps exe.

When I call execute call the HitVeinWin32API.Initialize function in the C#
app, the function returns a zero and the hVeinHandle IntPtr variable does
not contain the context handle from the dll. If I call
Marshal.GetLastWin32Error no error is returned. I have included some code
snippets below. Any help would be greatly appreicated.

Unmanaged code from HitVeinWin32.dll:

// This is the exported function for the CHitVeinWin32::Initialize function
extern "C" HITVEINWIN32_API BOOL Initialize(IN OUT HVEINCONTEXT
*phVeinContext);

HITVEINWIN32_API BOOL Initialize(HVEINCONTEXT *phVeinContext)
{

*phVeinContext = (HVEINCONTEXT)NULL;
CHitVeinWin32* pContext = new CHitVeinWin32();

if(!pContext)
return FALSE;

if(!pContext->Initialize())
{
delete pContext;
return FALSE;
}

*phVeinContext = (HVEINCONTEXT)pContext;

return TRUE;
}

Managed code from C# web service:

[DllImport("HitVeinWin32.dll", EntryPoint="Initialize",
CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool Initialize(ref IntPtr phVeinContext);

private void button1_Click(object sender, EventArgs e)
{
IntPtr hVeinHandle;
int iLastError;
string error_message;
int error_returned;
try
{
error_returned = 0;
error_message = "";
hVeinHandle = new IntPtr(0);
if (HitVeinWin32API.Initialize(ref hVeinHandle))
{
error_message = "";
}
else
{
if (hVeinHandle == IntPtr.Zero)
{

error_returned = Marshal.GetLastWin32Error();
if (error_returned == 0)
error_returned = 0x8000908;
error_message =
AvionBioMetricsError.GetErrorMessage(error_returned);
}
else
{
iLastError =
HitVeinWin32API.ErrorNumber(hVeinHandle);
error_message =
AvionBioMetricsError.GetErrorMessage(iLastError);
error_returned = iLastError;
HitVeinWin32API.ReleaseContext(hVeinHandle);
}
}
}
catch (Exception ex)
{
error_returned = 0x8000999;
error_message =
AvionBioMetricsError.GetErrorMessage(error_returned);
error_message += " " + ex.Message.ToString();
}
}
Marc Oden
2010-02-05 13:30:53 UTC
Permalink
Setting the project to enable unmanaged code debugging lead me to the
problem.
Post by Marc Oden
I have written a unmanaged C++ dl that I am attempting to call from a C#
app. The dll (HitVeinWin32.dll) is in the same directory as the C# apps exe.
When I call execute call the HitVeinWin32API.Initialize function in the C#
app, the function returns a zero and the hVeinHandle IntPtr variable does
not contain the context handle from the dll. If I call
Marshal.GetLastWin32Error no error is returned. I have included some code
snippets below. Any help would be greatly appreicated.
// This is the exported function for the CHitVeinWin32::Initialize function
extern "C" HITVEINWIN32_API BOOL Initialize(IN OUT HVEINCONTEXT
*phVeinContext);
HITVEINWIN32_API BOOL Initialize(HVEINCONTEXT *phVeinContext)
{
*phVeinContext = (HVEINCONTEXT)NULL;
CHitVeinWin32* pContext = new CHitVeinWin32();
if(!pContext)
return FALSE;
if(!pContext->Initialize())
{
delete pContext;
return FALSE;
}
*phVeinContext = (HVEINCONTEXT)pContext;
return TRUE;
}
[DllImport("HitVeinWin32.dll", EntryPoint="Initialize",
CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool Initialize(ref IntPtr phVeinContext);
private void button1_Click(object sender, EventArgs e)
{
IntPtr hVeinHandle;
int iLastError;
string error_message;
int error_returned;
try
{
error_returned = 0;
error_message = "";
hVeinHandle = new IntPtr(0);
if (HitVeinWin32API.Initialize(ref hVeinHandle))
{
error_message = "";
}
else
{
if (hVeinHandle == IntPtr.Zero)
{
error_returned = Marshal.GetLastWin32Error();
if (error_returned == 0)
error_returned = 0x8000908;
error_message =
AvionBioMetricsError.GetErrorMessage(error_returned);
}
else
{
iLastError =
HitVeinWin32API.ErrorNumber(hVeinHandle);
error_message =
AvionBioMetricsError.GetErrorMessage(iLastError);
error_returned = iLastError;
HitVeinWin32API.ReleaseContext(hVeinHandle);
}
}
}
catch (Exception ex)
{
error_returned = 0x8000999;
error_message =
AvionBioMetricsError.GetErrorMessage(error_returned);
error_message += " " + ex.Message.ToString();
}
}
Loading...