473,811 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# access to C DLL

Here's the breakdown:

I have an old C DLL that I want to access via C#.

I'm doing this via an outer DLL that wraps the old C DLL in an unmanaged
C++ class, which is in turn wrapped in a Managed C++ class. Both these
unmanaged C++ and managed C++ classes are compiled into a single assembly
(DLL) that is accessed by C#.

[C DLL] <--> [Unmanaged C++ / Managed C++] <--> [C# app]
old dll new C++ assembly app

I don't want to use standard Interop methods. I'm trying to do all the
direct access to the old C DLL with unmanaged C++. The data types in the
old C DLL are relatively complex, so I'm encapsulating the cluttered
legacy stuff in the new unmanaged C++ class.

IOW, I'll usually only be passing ints and a couple strings between the
unmanaged and managed sides.

In general, this approach works, but I'm getting occasional runtime errors
that I can't explain. Could someone post or refer me to a very simple
example that does this with even a single function? Say, take a function
from a stock Windows DLL and show a correct unmanaged C++ class
and managed C++ class that will wrap it correctly.

Thanks for any insights you can provide.
Nov 16 '05 #1
6 1905
On Sun, 09 Jan 2005 21:50:59 -0500, _BNC <_B**@nospam.co m> wrote:
Here's the breakdown:

I have an old C DLL that I want to access via C#.

I'm doing this via an outer DLL that wraps the old C DLL in an unmanaged
C++ class, which is in turn wrapped in a Managed C++ class. Both these
unmanaged C++ and managed C++ classes are compiled into a single assembly
(DLL) that is accessed by C#.

[C DLL] <--> [Unmanaged C++ / Managed C++] <--> [C# app]
old dll new C++ assembly app

I don't want to use standard Interop methods.
Oops. I meant I don't want to use PInvoke. I'm using the flat 'IJW'
model.
I'm trying to do all the
direct access to the old C DLL with unmanaged C++. The data types in the
old C DLL are relatively complex, so I'm encapsulating the cluttered
legacy stuff in the new unmanaged C++ class.

IOW, I'll usually only be passing ints and a couple strings between the
unmanaged and managed sides.

In general, this approach works, but I'm getting occasional runtime errors
that I can't explain. Could someone post or refer me to a very simple
example that does this with even a single function? Say, take a function
from a stock Windows DLL and show a correct unmanaged C++ class
and managed C++ class that will wrap it correctly.

Thanks for any insights you can provide.


Nov 16 '05 #2
_BNC,

The example isn't going to be that complex, I am afraid. Actually, you
don't even need code to show how to call the C code from the unmanaged C++
code. The issue is properly handling the unmanaged C++ class (since it is
allocated on the unmanaged heap, etc, etc.).

Because your class will have an unmanaged resource in it (the unmanaged
C++ class), you should implement the IDispose interface, so that the Dispose
method can be exposed, and the class can be cleaned up correctly.

For more information on doing this sort of thing, check out the section
of the .NET framework titled "Managed Extensions for C++ Migration Guide",
located at (watch for line wrap):

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

Also, if you have time, and think that you can use the CLI extensions
(as managed extensions are going out the window), you might want to do that,
as it would be much easier, from what I hear.

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

"_BNC" <_B**@nospam.co m> wrote in message
news:8n******** *************** *********@4ax.c om...
On Sun, 09 Jan 2005 21:50:59 -0500, _BNC <_B**@nospam.co m> wrote:
Here's the breakdown:

I have an old C DLL that I want to access via C#.

I'm doing this via an outer DLL that wraps the old C DLL in an unmanaged
C++ class, which is in turn wrapped in a Managed C++ class. Both these
unmanaged C++ and managed C++ classes are compiled into a single assembly
(DLL) that is accessed by C#.

[C DLL] <--> [Unmanaged C++ / Managed C++] <--> [C# app]
old dll new C++ assembly app

I don't want to use standard Interop methods.


Oops. I meant I don't want to use PInvoke. I'm using the flat 'IJW'
model.
I'm trying to do all the
direct access to the old C DLL with unmanaged C++. The data types in the
old C DLL are relatively complex, so I'm encapsulating the cluttered
legacy stuff in the new unmanaged C++ class.

IOW, I'll usually only be passing ints and a couple strings between the
unmanaged and managed sides.

In general, this approach works, but I'm getting occasional runtime errors
that I can't explain. Could someone post or refer me to a very simple
example that does this with even a single function? Say, take a function
from a stock Windows DLL and show a correct unmanaged C++ class
and managed C++ class that will wrap it correctly.

Thanks for any insights you can provide.

Nov 16 '05 #3
[re 'flat model / C++ interop' bridge to unmanaged DLL]

On Mon, 10 Jan 2005 10:14:01 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard .caspershouse.c om> wrote:
The example isn't going to be that complex, I am afraid. Actually, you
don't even need code to show how to call the C code from the unmanaged C++
code. The issue is properly handling the unmanaged C++ class (since it is
allocated on the unmanaged heap, etc, etc.). Because your class will have an unmanaged resource in it (the unmanaged
C++ class), you should implement the IDispose interface, so that the Dispose
method can be exposed, and the class can be cleaned up correctly.
I've done all unmanaged mem management (ahem) inside the unmanaged C++
wrapper. I haven't used 'dispose', as I manually keep track of all the
alloc'd memory. I don't think that's the problem, especially in that
there are only a couple large buffers that need to be accounted for. I'll
give that another look, but the code problems occur before the dealloc of
any objects.

The problem IS that the bridge between unmanaged and managed is somewhat
of a black box to me. I don't have time on this project to study the
Marshal class as thoroughly as I'd like, nor to review the sparse examples
that I've seen for 'flat model'. I'm afraid I'm missing something.

Despite that, the only thing I'm passing between managed/unmanaged is a
few pointers, and the code that I was drawing from did not use Marshal for
transfering simple (int, short) types.

The other puzzling thing is that failures are intermittent. It would seem
that (short of a sweep of the memory manager) they should either fail or
succeed. The failure rate seems independent of machine speed or amount of
available ram.

With enough study time and clues, I'd be able to solve this easily, but
unfortunately, the project manager doesn't have much patience for a
methodical approach.
For more information on doing this sort of thing, check out the section
of the .NET framework titled "Managed Extensions for C++ Migration Guide",
located at (watch for line wrap):

http://msdn.microsoft.com/library/de...nagedtypes.asp
Thanks for the pointer.
Also, if you have time, and think that you can use the CLI extensions
(as managed extensions are going out the window),


!? Uh-oh. I've done a search for CLI Extensions and come up with only a
handful of google hits. Is this part of VS 2005, or is it something that
is currently accessible?

Thanks, Nicholas.

B

Nov 16 '05 #4
_BNC,

When I said that you would want to expose a Dispose method on the
managed end, it wasn't because of the memory management you use in the
unmanaged code, but for the class you use to wrap that code. Simply, if
your class has all the operations exposed as methods, then you are going to
create and store an instance of that class in the managed wrapper. You
should have a Dispose method on the managed wrapper so that it can dispose
of the unmanaged memory as soon as possible.

Now it could be argued that if you delete the unmanaged memory properly
in the finalizer, then the CLR will handle the memory management. However,
it isn't aware of the pressure that you place on the system to allocate that
class (or the buffers you manage), so it's probably better to expose an
IDisposable implementation.

If you are creating instances of these classes (using auto pointers) and
using them only in your methods, and not storing them on the class level,
then the IDisposable implementation is not needed.

As for CLI, check out the section of the Visual C++ Developers Center
titled "Translatio n Guide: Moving Your Programs from Managed Extensions for
C++ to C++/CLI", located at (watch for line wrap):

http://msdn.microsoft.com/visualc/wh...transguide.asp
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"_BNC" <_B**@nospam.co m> wrote in message
news:cf******** *************** *********@4ax.c om...
[re 'flat model / C++ interop' bridge to unmanaged DLL]

On Mon, 10 Jan 2005 10:14:01 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard .caspershouse.c om> wrote:
The example isn't going to be that complex, I am afraid. Actually,
you
don't even need code to show how to call the C code from the unmanaged C++
code. The issue is properly handling the unmanaged C++ class (since it is
allocated on the unmanaged heap, etc, etc.).

Because your class will have an unmanaged resource in it (the
unmanaged
C++ class), you should implement the IDispose interface, so that the
Dispose
method can be exposed, and the class can be cleaned up correctly.


I've done all unmanaged mem management (ahem) inside the unmanaged C++
wrapper. I haven't used 'dispose', as I manually keep track of all the
alloc'd memory. I don't think that's the problem, especially in that
there are only a couple large buffers that need to be accounted for. I'll
give that another look, but the code problems occur before the dealloc of
any objects.

The problem IS that the bridge between unmanaged and managed is somewhat
of a black box to me. I don't have time on this project to study the
Marshal class as thoroughly as I'd like, nor to review the sparse examples
that I've seen for 'flat model'. I'm afraid I'm missing something.

Despite that, the only thing I'm passing between managed/unmanaged is a
few pointers, and the code that I was drawing from did not use Marshal for
transfering simple (int, short) types.

The other puzzling thing is that failures are intermittent. It would seem
that (short of a sweep of the memory manager) they should either fail or
succeed. The failure rate seems independent of machine speed or amount of
available ram.

With enough study time and clues, I'd be able to solve this easily, but
unfortunately, the project manager doesn't have much patience for a
methodical approach.
For more information on doing this sort of thing, check out the
section
of the .NET framework titled "Managed Extensions for C++ Migration Guide",
located at (watch for line wrap):

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


Thanks for the pointer.
Also, if you have time, and think that you can use the CLI extensions
(as managed extensions are going out the window),


!? Uh-oh. I've done a search for CLI Extensions and come up with only a
handful of google hits. Is this part of VS 2005, or is it something that
is currently accessible?

Thanks, Nicholas.

B

Nov 16 '05 #5
Can't help you if you don't show us some code (preferably a complete sample
that illustrates the problem) and/or more details about the run errors.
My guess is that you are passing un-pinned pointers to unmanaged code.

Willy.
"_BNC" <_B**@nospam.co m> wrote in message
news:7m******** *************** *********@4ax.c om...
Here's the breakdown:

I have an old C DLL that I want to access via C#.

I'm doing this via an outer DLL that wraps the old C DLL in an unmanaged
C++ class, which is in turn wrapped in a Managed C++ class. Both these
unmanaged C++ and managed C++ classes are compiled into a single assembly
(DLL) that is accessed by C#.

[C DLL] <--> [Unmanaged C++ / Managed C++] <--> [C# app]
old dll new C++ assembly app

I don't want to use standard Interop methods. I'm trying to do all the
direct access to the old C DLL with unmanaged C++. The data types in the
old C DLL are relatively complex, so I'm encapsulating the cluttered
legacy stuff in the new unmanaged C++ class.

IOW, I'll usually only be passing ints and a couple strings between the
unmanaged and managed sides.

In general, this approach works, but I'm getting occasional runtime errors
that I can't explain. Could someone post or refer me to a very simple
example that does this with even a single function? Say, take a function
from a stock Windows DLL and show a correct unmanaged C++ class
and managed C++ class that will wrap it correctly.

Thanks for any insights you can provide.

Nov 16 '05 #6
>> I'm doing this via an outer DLL that wraps the old C DLL in an unmanaged
C++ class, which is in turn wrapped in a Managed C++ class. Both these
unmanaged C++ and managed C++ classes are compiled into a single assembly
(DLL) that is accessed by C#.

[C DLL] <--> [Unmanaged C++ / Managed C++] <--> [C# app]
old dll new C++ assembly app

I don't want to use standard Interop methods. I'm trying to do all the
direct access to the old C DLL with unmanaged C++.

On Mon, 10 Jan 2005 21:49:06 +0100, "Willy Denoyette [MVP]"
<wi************ *@pandora.be> wrote:
Can't help you if you don't show us some code (preferably a complete sample
that illustrates the problem) and/or more details about the run errors.
My guess is that you are passing un-pinned pointers to unmanaged code.


Yeah, I was hoping that there was a simple code illustration somewhere.
I've seen many examples for PInvoke, but very little info on wrapping
unmanaged code, especially with both unmanaged and managed C++
classes in the same module. (Are there no code samples that do this?
I would have thought it was a common thing to do)

I will try to work up a simple example to post, but it will probably work
OK. The actual app is complex, and apparently only turns up problems
when stressed.

Still, your comment about pinning pointers may well be the main problem.

I've bought plenty of books on the subject (Grimes, Troelsen, etc) but as
I mentioned, my client won't afford the necessary time to dig into it (I
bet no one has heard that before). I believe it will take more more time
to NOT dig into the internals of this, but I don't have the luxury. I do
have a feeling that my initial reference example may have taken some
shortcuts.

Re pinning, a couple Q's come to mind:

Wouldn't pointer corruption only occur when the GC runs? Otherwise,
I can't imagine why a managed pointer would be altered.

How often does the garbage collector normally run on a medium-sized app?
(Presuming that lots of RAM is available)

Any recommended references that address this directly?

_B

Nov 16 '05 #7

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

Similar topics

63
5949
by: Jerome | last post by:
Hi, I'm a bit confused ... when would I rather write an database application using MS Access and Visual Basic and when (and why) would I rather write it using Visual Studio .Net? Is it as easy in Visual Studio to create reports and labels as it's in Access?` The advantage of VS.net is that not every user needs Access, right? And that would eliminate the Access version problem as well I guess.
13
2959
by: bill | last post by:
I am trying to convince a client that dotNet is preferable to an Access project (ADP/ADE). This client currently has a large, pure Access MDB solution with 30+ users, which needs to be upgraded. I believe a dotNet solution is better, but I'm trying to be as convincing as possible -- and maybe I'm wrong! I would appreciate any input or references which could help me.
1
4350
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an inherited base member's access level in the derived class: its access level in the base class and the type of inheritance (public, protected, or private). After this determination is made, the following possibilities exist for manually changing the...
13
13364
by: Simon Bailey | last post by:
I am a newcomer to databases and am not sure which DBMS to use. I have a very simplified knowledge of databases overall. I would very much appreciate a (simplifed) message explaining the advantages and disadvantages of both programs. Many Thanks Simon
0
2987
by: Frederick Noronha \(FN\) | last post by:
---------- Forwarded message ---------- Solutions to Everyday User Interface and Programming Problems O'Reilly Releases "Access Cookbook, Second Edition" Sebastopol, CA--Neither reference book nor tutorial, "Access Cookbook, Second Edition" (O'Reilly, US $49.95), by Ken Getz, Paul Litwin, and Andy Baron, delivers hundreds of practical examples, up-to-date suggestions, and handy solutions to real-world problems that Access users and...
20
3354
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to speed. I like books with practical exercises, and also with test questions (like cert books) *2*
64
5269
by: John | last post by:
Hi What future does access have after the release of vs 2005/sql 2005? MS doesn't seem to have done anything major with access lately and presumably hoping that everyone migrates to vs/sql. Any comments? Thanks
1
3352
by: com | last post by:
Extreme Web Reports 2005 - Soft30.com The wizard scans the specified MS Access database and records information such as report names, parameters and subqueries. ... www.soft30.com/download-1-11975.htm - 31k - Cached - Similar pages MDBSecure 1.0.8.0 - Soft30.com Utility which makes it easy to create secure MS Access Databases, ... MS Access 2000/2003 format. 30 day money back guarantee, 30 day trial. ...
17
4425
by: Mell via AccessMonster.com | last post by:
Is there a way to find out where an application was created from? i.e. - work or home i.e. - if application sits on a (work) server/network, the IT people know the application is sitting there, but is there a way they can find out if that application was put there from a CD or email or created at work? Hint: It's not on a client/server database, just native jet database mdb created on Access 2003 (default 2000)...
37
5273
by: jasmith | last post by:
How will Access fair in a year? Two years? .... The new version of Access seems to service non programmers as a wizard interface to quickly create databases via a fancy wizard. Furthermore, why would you even continue to use Access as a backend when you have a much superior option in SQL express? What about as a future front-end development tool? Let's get serious. Microsoft continues to publish numerous articles and videos on how you...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
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
10386
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
10133
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
6889
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
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.