473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

import data from an unmanaged dll

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

Nov 15 '05 #1
3 5176
On 2004-01-22, Mark Urish <an*******@discussions.microsoft.com> wrote:
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
--
Tom Shelton [MVP]
Nov 15 '05 #2
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" <an*******@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
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


Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.com...
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.
Mark Urish


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank | last post by:
Hello, i have a problem with the data exchange between a managed C# application and an unmanged C++ DLL. The C# app fills some values of a structure and calls the C++ DLL. This DLL reads the...
4
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code into the Visual C#.NET. My C++ code is mainly based on the serial communication. So I am using the windows structure such as DCB.. etc and the windows...
0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
0
by: kagorami | last post by:
Q: How do you return a LPSTR from unmanaged code? A: Use a StringBuilder. What about this: public struct CSTRUCT { string return_value; } How do you get a LPSTR from unmanaged code when it...
2
by: amirbehzadan | last post by:
Hello, I am writing some C++ classes and want to export them as .dll files so other users can import them and use the methods I have provided in those classes. I have two types of classes :...
3
by: amirbehzadan | last post by:
Hello, I am writing some C++ classes and want to export them as .dll files so other users can import them and use the methods I have provided in those classes. I have two types of classes :...
4
by: Zytan | last post by:
I have VB code that shows how to import a DLL function. I compiled it. I used .NET Reflector to see the C# code, and it shows this: private static extern bool MethodName( ref string...
3
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm trying to specify the requirement from unmanaged DLL component that will be used by a managed application written in C#. The unmanaged DLL is implementing some kind of algorithm for defect...
1
by: madbat87 | last post by:
Hi, I'm trying to import code from a c++ unmanaged dll in a c# project. In the dll there's a class named Connection, but the dll exposes only 2 methods (outside the class): - HBITMAP...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.