473,804 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DLL Development in C# - Exporting functions

Hi there,

I have the following problem. At the moment I am creating an
application that interfaces with several hardware modules via USB.
Each of these devices has its own functionality and I want to make
this functionality available to programmers in a very generic way.
Therefore I came up with the following idea.

1) USB devices can have a small description. In this description i
will store the name of the DLL that covers the device its
functionality.

2) When the USB device is plugged in, the application will load the
DLL stored in the device. This will only be generic functions that
count for all devices.

3) These DLL's then provide some generic functions that enable
programmers to load the device specific functions from the DLL.

Im am sure this will work, I know how to do it in C, but my managers
want me to develop this in C#.NET. I read some articles on DLL's in C#
and I know how to write some basic DLL's in C#, but I couldn't find
the dynamics I need for this solution. Is it even possible in C# and
how can it be done?

Thank you in advance!!

Stijn
Nov 15 '05 #1
6 10260
Stijn,

You won't be able to do this with C#. In .NET, you can not create
assemblies that export functions, as you can do in C.

If you want to do this, then you will have to create a C wrapper which
will subsequently make calls into a .NET managed assembly. At that point,
you can use C# to interface with the C code that is called.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Stijn" <xi***@ht-security.net> wrote in message
news:80******** *************** ***@posting.goo gle.com...
Hi there,

I have the following problem. At the moment I am creating an
application that interfaces with several hardware modules via USB.
Each of these devices has its own functionality and I want to make
this functionality available to programmers in a very generic way.
Therefore I came up with the following idea.

1) USB devices can have a small description. In this description i
will store the name of the DLL that covers the device its
functionality.

2) When the USB device is plugged in, the application will load the
DLL stored in the device. This will only be generic functions that
count for all devices.

3) These DLL's then provide some generic functions that enable
programmers to load the device specific functions from the DLL.

Im am sure this will work, I know how to do it in C, but my managers
want me to develop this in C#.NET. I read some articles on DLL's in C#
and I know how to write some basic DLL's in C#, but I couldn't find
the dynamics I need for this solution. Is it even possible in C# and
how can it be done?

Thank you in advance!!

Stijn

Nov 15 '05 #2
Nicholas,

see inline ***

Willy.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:Oi******** ********@TK2MSF TNGP11.phx.gbl. ..
Stijn,

You won't be able to do this with C#. In .NET, you can not create
assemblies that export functions, as you can do in C.

Actually you can, using ILASM , but this is something you should never do :-).
Just consider following sample.
// unmexports.il
// Compile with : ilasm unmexports.il /dll
assembly extern mscorlib {}
..assembly UnmExports {}
..module UnmExports.dll
// This flag is important
..corflags 0x00000002
// This instructs the CLR to create a marshaling thunk for the unmanaged caller
..vtfixup [1] int32 fromunmanaged at VT_01
..data VT_01 = int32(0)
..method public static void foo()
{
..vtentry 1:1
..export [1] as foo
ldstr "Hello from managed world"
call void [mscorlib]System.Console: :WriteLine(stri ng)
ret
}
---------------------------------------------------------------
// File: Callmngd.cpp
// Call's a managed function through the managed assembly's export table
//#include <stdio.h>
#include <windows.h>
// compile using: cl Callmngd.cpp
typedef void (*FOOPROC)(void );

void main(void)
{
HINSTANCE hinstLib;
FOOPROC FooAddress;
bool res = false;
// Get a handle to the DLL module.
hinstLib = LoadLibrary("un mexports");
// If the handle is valid, try to get the function address.
if (hinstLib != 0)
{
FooAddress = (FOOPROC) GetProcAddress( hinstLib, "foo");
if (0 != FooAddress)
{
// Call managed function
FooAddress ();
}
// Free the DLL module.
res = FreeLibrary(hin stLib);
}
}
Willy.


Nov 15 '05 #3
Willy,
but this is something you should never do :-).


Why do you say that? It has its uses.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4
Mattias,
Let's say I meant "IMO you should consider the alternatives before going down that road", but I agree, it may have it's uses.

The problem with this solution is that, currently (AFAIK), none of the high level languages (C#, VB.NET etc...) do expose the
managed methods as unmanaged exports, so your only option is to program in ILAsm or to round trip a piece of code written in a high
level language.
Round tripping is rather cumbersome, and, its not easy to write any reasonable sized application using ILAsm.

As first alternative there is MC++ which offers a perfect solution when one needs to mix unmanaged and managed (C++) code.
When you need to call managed methods from other unmanaged languages, and you don't mind to create an extra layer, you could write
a wrapper in MC++ and export the unmanaged functions that call the methods, or you could expose your managed classes as COM
interfaces to be consumed by COM clients.

Willy.

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message news:us******** ******@tk2msftn gp13.phx.gbl...
Willy,
but this is something you should never do :-).


Why do you say that? It has its uses.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #5
Willy,
Let's say I meant "IMO you should consider the alternatives before going down that road", but I agree, it may have it's uses.


OK cool, then we agree :)

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #6
Stijn,

You may want to look at Eric Gunnerson's article, "AppDomains and
Dynamic Loading":

http://msdn.microsoft.com/library/de...rp05162002.asp

It's not as easy as the old LoadLibrary() and GetProcAddr(), and it may
only get you to the generic functions (an interface in .Net terms), but
you may be able to re-architect to access the specific functions within
this framework...

--
Steve Rosenberry
Sr. Partner

Electronic Solutions Company -- For the Home of Integration
http://ElectronicSolutionsCo.com

(610) 670-1710

Nov 15 '05 #7

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

Similar topics

70
5117
by: KingIshu | last post by:
Hi All, I am developing an object oriented OS code-named "ikbocs". 1) What are the pros and cons of the following languages interms of Effectiveness on System Programming, Object Orientedness etc ? (Simula, Smalltalk, Modula-3, Eiffel, Sather, C++) I suppose Java is not suitable for Sys. Programming. 2) Which OOP language would be better for OS development? TIA
3
3970
by: chetan | last post by:
Hi , myself Chetan Is There anybody could help me ? I am working on the project in c++ ,, I am in great confusion that should I export c++ member functions OR methods to create objects of that class OR previously cretaed objects .. So those can be used by users by using the header file ... of my " class "
2
7714
by: G | last post by:
When I export data from access to excel by with "export" or "Analyze with" I seem to loose parts of some fields (long text strings). Is there a way to export it all to excel? Thanks G
4
2032
by: Alexander Filatov | last post by:
I need implementing Windows Cluster Resource DLL, that requires me creating DLL exporting several functions. How it can be done in C#? Thanks, Alexander
7
1897
by: victorsk | last post by:
Hello, I have a dll which I compiled in VB. Now I would like to use this dll in MapBasic program. However, I keep getting an error saying that the function which I am calling cannong be found. After doing some research, I learned that VB's functions are not exportable and suggestion is to get PowerBasic or XBasic. I am just starting with VB so I am not sure if I am right, but can somebody please tell me if I can make exportable...
0
302
by: victorsk | last post by:
Hello, I need to write a DLL app in VB whose functions would be called by another app. VB's DLL's don't support exporting functions. However, I found a site that provides a way to export VB functions. But, I have been having trouble making that compiler-linker to work. I am new to VB but what I understand is this app mimics a generic "link.exe" while applying a generic renamed "link.exe" called "link1.exe" by issuing commands to modify a...
6
8238
by: sara | last post by:
I have what I think is a little strange...I have to get data from our payroll system into a specific format (fixed record length) as a .txt or .prn file only to upload to our 401k custodian. I can get the data into Access (from Payroll) and write a query with all the fields (inserting fields with spaces, as needed), but I can't figure out how to get it to export to a .txt file - without the field name header row. I don't really ...
12
7261
by: 2b|!2b==? | last post by:
I want to export my C++ classes in a DLL, using ordinal # - rather than by name. Will anyone care to enumerate through the steps required to do this? I am already failiar with exporting classes and symbols (both C++ and C) from a DLL. In the case of C functions, i also know how to export them by ordinal # - my main problem revolves around how to do the ff: 1). Obtaining the mangled names from the C++ DLL 2). How to map them (if any...
1
570
by: Saad | last post by:
Hi, I have a MC++dll, whose functions i want to use in unmanaged c++. I read that one of the ways to use the managed functions in unmanaged world is by exposing the managed api as flat api. I tried that and with simple data types it worked. But now i need to export a function which needs to take a gc struct as parameter, and then fill some values in it, and then the unmanaged c++ will use that info.
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10564
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.