473,806 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using C# DLL from plain C

We write everything in plain old C. We have a potential customer that
wants to provide us a DLL written in C#. How can we call it's
functions?

Nov 17 '05 #1
5 3784
It's not very hard at all.

You will have to make calls from unmanaged code into managed code. Its not
real hard, but I recommend at least VS2003. Basically all you will need to
do is a import your dll into the project, setup the name space, and use a
few special macros and keywords to setup your managed code.

Check out this article on Code Project on how to do it.
(http://www.codeproject.com/managedcp...to_managed.asp)

Hope this helps...

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"dvtaylor" <dv******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
We write everything in plain old C. We have a potential customer that
wants to provide us a DLL written in C#. How can we call it's
functions?

Nov 17 '05 #2
GTi
Ok, that looks like C++, But what about pure C ?
"Frisky" <Fr***********@ NorthPole.net> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
It's not very hard at all.

You will have to make calls from unmanaged code into managed code. Its not
real hard, but I recommend at least VS2003. Basically all you will need to
do is a import your dll into the project, setup the name space, and use a
few special macros and keywords to setup your managed code.

Check out this article on Code Project on how to do it.
(http://www.codeproject.com/managedcp...to_managed.asp)

Hope this helps...

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"dvtaylor" <dv******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
We write everything in plain old C. We have a potential customer that
wants to provide us a DLL written in C#. How can we call it's
functions?


Nov 17 '05 #3
Besides the wrapper solution as mentiond by Frisky, you can call C# from
unmanaged C through COM introp.
If you have a C++ system that has built-in support for COM (all VC++
compilers have this), all you have to do is design your C# classes you want
to expose to COM to be "COM friendly". From this you can build a typelib
(using regasm and/or tlbexp) that can be imported in your C code (#import
compiler directive), your code can now use all COM support offered by the
C++ compiler.
However when using other C++ compilers (without COM support features) it's
much harder to do but it's not impossible.

Willy.

"GTi" <gt*@gti.com> wrote in message news:42******** @news.wineasy.s e...
Ok, that looks like C++, But what about pure C ?
"Frisky" <Fr***********@ NorthPole.net> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
It's not very hard at all.

You will have to make calls from unmanaged code into managed code. Its
not real hard, but I recommend at least VS2003. Basically all you will
need to do is a import your dll into the project, setup the name space,
and use a few special macros and keywords to setup your managed code.

Check out this article on Code Project on how to do it.
(http://www.codeproject.com/managedcp...to_managed.asp)

Hope this helps...

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"dvtaylor" <dv******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
We write everything in plain old C. We have a potential customer that
wants to provide us a DLL written in C#. How can we call it's
functions?



Nov 17 '05 #4
Willy has a good idea about using COM Interop to do it.

From a pure "C" perspective you can't do it. "C" has no notion of
namespaces, and .Net relies on them heavily. I base this on trying one of my
good old C++ "Hello World" pieces written in managed code and changing the
extension of the file from ".cpp" to ".c". For 3 lines of code I 20 errors
and numerous warnings.

Is there a problem with having a single file of C++ code mixed in with your
"C"? What about an external DLL written in C++ that acts as a shim to the
"C" code. The C++ code you have to write is really a lot like newer "C" code
(not like old K&R stuff). Here is a sample:
#using <mscorlib.dll >
#using <system.dll>

using namespace System;

void main()
{
String * str = new String(L"Now is the time for all good men to come to the
aid of their country.");
str = str->ToUpper();

Console::WriteL ine(str);
}

Note: You could just as easily import a DLL you wrote in C# and call it's
methods. I just used the Microsoft supplied System dll as an example.

(BTW: Don't take this the wrong way, but since I don't know how much of the
..Net stuff you are familiar with: The String is the System::String object
from the System.dll. Also, Console::WriteL ine() is
System::Console ::WriteLine() from System.dll. And, the String I new'ed will
automatically be garbage collected by the .Net framework since it is a __gc
class from the framework. So, I don't have to delete it.)

Otherwise, it looks like you are talking about Willy's idea of COM.

Hope this helps...

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"GTi" <gt*@gti.com> wrote in message news:42******** @news.wineasy.s e...
Ok, that looks like C++, But what about pure C ?
"Frisky" <Fr***********@ NorthPole.net> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
It's not very hard at all.

You will have to make calls from unmanaged code into managed code. Its
not real hard, but I recommend at least VS2003. Basically all you will
need to do is a import your dll into the project, setup the name space,
and use a few special macros and keywords to setup your managed code.

Check out this article on Code Project on how to do it.
(http://www.codeproject.com/managedcp...to_managed.asp)

Hope this helps...

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"dvtaylor" <dv******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
We write everything in plain old C. We have a potential customer that
wants to provide us a DLL written in C#. How can we call it's
functions?



Nov 17 '05 #5
Frisky wrote:

We are talking about C calling managed code.

C does not understand *anything* about managed code, it does not know
what managed code is, or anything about how it manages objects.
Willy has a good idea about using COM Interop to do it.
This is the *only* way to call managed objects from C because it is the
only way to initialize the runtime correctly and get access to a managed
object that uses .NET's thunking layers.
From a pure "C" perspective you can't do it. "C" has no notion of
namespaces, and .Net relies on them heavily. I base this on trying
This is irrelevant because C cannot access managed objects. For a start,
all methods on managed objects use a special calling convention called
__clrcall and C does not understand this. (Try it and see <g>, BTW this
calling convention can be used in managed C++ in .NET 2.0, but not by
earlier versions).
one of my good old C++ "Hello World" pieces written in managed code
and changing the extension of the file from ".cpp" to ".c". For 3
lines of code I 20 errors and numerous warnings.
Again, this is irrelevant. The managed extensions are for *C++* not for
C, there is no concept of Managed C (from Microsoft).
Is there a problem with having a single file of C++ code mixed in
with your "C"? What about an external DLL written in C++ that acts as
a shim to the "C" code. The C++ code you have to write is really a
lot like newer "C" code (not like old K&R stuff). Here is a sample:


Let's me re-assert what the OP was saying - dvtaylor wanted to access
code in a managed assembly library written in C#, the call would be made
from C. Thus the call is made from unmanaged code to managed code. The
example from codeproject was managed code calling unmanaged code (and
not very well, since there is a memory leak in the code).

Richard
--
www.richardgrimes.com
my email ev******@zicf.b et is encrypted with ROT13 (www.rot13.org)
Nov 17 '05 #6

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

Similar topics

1
6880
by: M Wells | last post by:
Hi All, I'm trying to implement encryption on certain data fields in my MySQL database and I'm experiencing ongoing problems. I seem to be able to encrypt the data without issues, but can't figure out how to decrypt the data. My field in my test table, in which I'm storing the encrypted data, is a TEXT type field.
6
4845
by: Billy Jacobs | last post by:
I have a website which has both secure and non-secure pages. I want to uses forms authentication. How do I accomplish this? Originally I had my web.config file in the root with Forms Authentication set up and it worked just fine. Then I realized that I needed to have some pages unsecure. I then created 2 directories. One named Secure and the other named Public. I placed my web.config file in my
4
2566
by: Gactimus | last post by:
Here is a program that encodes and decodes a text file. What I need to do is write a C++ program that requests 3 different file names. One filename is for the source file to be encoded, another is the name of the 'encoded' output file, and the final filename is an offset file. On a character by character basis, the program needs to look at the first character of the source file and offset it by the first character in the offset file. The...
9
3976
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it will not set the value of the control and will leave the checked status of the checkbox to false when a user selects a new date. this works fine when using the control on a win2k machine but if we use it on a win XP box and call
15
4784
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is not validate based on some given rules. I also have a custom error handling page to show the...
8
1535
by: Mike Owen | last post by:
Hi, I am trying to pre-compile a project prior using ASP.Net 2.0, VS 2005, to putting it onto a live server. The reason for doing this is that other people have access to the server, and I thereofre want to keep the code secure. If I use the 'Build/Publish Web Site' option, it asks me to tell it the 'Target location' which in this case is 'C:\Temp\PrecompiledWeb\Project1'
34
13245
by: John | last post by:
This produces an initialized array to zero: int *i = new int() ; 004124B0 push ebp 004124B1 mov ebp,esp 004124B3 mov eax,dword ptr 004124B6 push eax 004124B7 call operator new (411212h) 004124BC add esp,4
0
6349
by: jlconde | last post by:
I have a classe to send mails. It runs on yahoo well but with hotmail I never receive the mails.I do not receive an error neither. I would need some strange header to make the hotmail like my email???? My code is: import java.net.*; import java.io.*; import java.util.*;
3
5082
by: Tracey | last post by:
Is there a way to include the quote mark/character when using the MessageBox.Show? In other words is there an alternative/secondary delimiter for strings in VB2005? <"denotes the desired use/position of the quote mark/character: MessageBox.Show( <"& txtText.Text & <"& " is plain text in quotes", "Text Box") The does not seem to include the quote mark
7
2834
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It seems to work fine, however I noticed that no web log entry is added when a successful download occurs (normally a 200 HTTP status code, however, if there is an authorization failure, it gets logged). I have a logging routine that logs a successful...
0
9719
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
9597
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
10366
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...
1
10371
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
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...
1
7649
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
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
3
3008
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.