import data from an unmanaged dll

G

Guest

Is there a way to import data from an unmanaged dll? For example,

unmanaged.dll:
__declspec(dllexport) int unmanagedErrno;

managed.cs:
class Managed
{
//this doesn't work:
[DllImport("unmanaged.dll")]
static extern int unmanagedErrno;
}

This fails because "extern" doesn't work for data, only methods. Is there a way to access "unmanagedErrno" in managed code?

Thanks,
Mark Urish
 
T

Tom Shelton

Is there a way to import data from an unmanaged dll? For example,

unmanaged.dll:
__declspec(dllexport) int unmanagedErrno;

managed.cs:
class Managed
{
//this doesn't work:
[DllImport("unmanaged.dll")]
static extern int unmanagedErrno;
}

This fails because "extern" doesn't work for data, only methods. Is there a way to access "unmanagedErrno" in managed code?

To be honest, I haven't tried it in Windows - but I do this on Linux
with mono using the dlopen/dlsym/dlclose system calls...

On windows, you could probably call LoadLibrary and then
GetProcAddress... Something like:

class Managed
{
[DllImport("kernel32", CharSet=CharSet.Auto, SetLastError=true)]
private static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32", CharSet=CharSet.Auto, SetLastError=true)]
private static extern IntPtr GetProcAddress(IntPtr hModule,
string lpProcName);

public static void Main()
{
int errNo = 0;
IntPtr address = IntPtr.Zero;
IntPtr hModule = LoadLibrary("unmanaged.dll");

if (hModule != IntPtr.Zero)
{
address = GetProcAddress(hModule, "unmanagedErrno");
if (address != IntPtr.Zero)
{
errNo = Marshal.ReadInt32(address);
Console.WriteLine(errNo);
}
else
{
Console.WriteLine("GetProcAddress Failed");
}
}
else
{
Console.WriteLine("LoadLibrary Failed");
}
}
}

HTH
 
D

Dmitriy Lapshin [C# / .NET MVP]

How would you do that in unmanaged code? If it can be done with plain API
calls, you could get an address of the data (say, with the GetProcAddress()
API) and then marshal the data to the managed space with the .NET's Marshal
class.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mark Urish said:
Is there a way to import data from an unmanaged dll? For example,

unmanaged.dll:
__declspec(dllexport) int unmanagedErrno;

managed.cs:
class Managed
{
//this doesn't work:
[DllImport("unmanaged.dll")]
static extern int unmanagedErrno;
}

This fails because "extern" doesn't work for data, only methods. Is there
a way to access "unmanagedErrno" in managed code?
 
D

Dmitriy Lapshin [C# / .NET MVP]

Mark,

Unfortunately, my knowledge of the Marshal class is merely theoretical
(except the ReleaseComObject method). I however think you can find examples
on the Marshal class in the MSDN Library.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mark Urish said:
We have traditionally used C/C++, where we could access the global dll data using

__declspec(dllimport) int unamanagedErrno;

This handled the LoadLibrary, GetProcAddress steps, and also the pointer
indirection. It seems like the "[DllImport] extern" serves a similar
purpose, so I was hoping there was something analogous for importing data.
Thanks for posting the example code. I am unfamiliar with the Marshal
functions, so your example should get me on the right track.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top