473,419 Members | 4,250 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,419 software developers and data experts.

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 3757
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.googlegr oups.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****************@TK2MSFTNGP10.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.googlegr oups.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.se...
Ok, that looks like C++, But what about pure C ?
"Frisky" <Fr***********@NorthPole.net> wrote in message
news:%2****************@TK2MSFTNGP10.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.googlegr oups.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::WriteLine(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::WriteLine() 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.se...
Ok, that looks like C++, But what about pure C ?
"Frisky" <Fr***********@NorthPole.net> wrote in message
news:%2****************@TK2MSFTNGP10.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.googlegr oups.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.bet 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
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...
6
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...
4
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...
9
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...
15
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...
8
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...
34
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...
0
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...
3
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...
7
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.