Discussion:
Problem calling unmanaged dll from c#
(too old to reply)
Marc Oden
2010-02-03 17:20:59 UTC
Permalink
A have written a unmanaged C++ dl that I am attempting to call from a .NET
2.0 Winform app written in C#. The dll (HitVeinWin32.dll) is in the same
directory as the web server dll.

When I call execute call the HitVeinWin32API.Initialize function in the web
service, 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.

Unmanged 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();
}
}
Jeroen Mostert
2010-02-03 19:49:46 UTC
Permalink
Post by Marc Oden
A have written a unmanaged C++ dl that I am attempting to call from a
.NET 2.0 Winform app written in C#. The dll (HitVeinWin32.dll) is in the
same directory as the web server dll.
When I call execute call the HitVeinWin32API.Initialize function in the
web service, 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;
}
Based on this code, it would seem that the call to
CHitVeinWin32::Initialize() fails, no? If that call does not call
SetLastError(), then GetLastError() won't tell you anything useful. You will
need to ask the author of this code to provide something a little more
useful than a simple negative return value.
--
J.
Marc Oden
2010-02-05 13:31:37 UTC
Permalink
Thank you for the response.

Setting the project to enable unmanaged code debugging lead me to the
problem.
Post by Jeroen Mostert
Post by Marc Oden
A have written a unmanaged C++ dl that I am attempting to call from a
.NET 2.0 Winform app written in C#. The dll (HitVeinWin32.dll) is in the
same directory as the web server dll.
When I call execute call the HitVeinWin32API.Initialize function in the
web service, 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;
}
Based on this code, it would seem that the call to
CHitVeinWin32::Initialize() fails, no? If that call does not call
SetLastError(), then GetLastError() won't tell you anything useful. You
will need to ask the author of this code to provide something a little
more useful than a simple negative return value.
--
J.
Loading...