473,395 Members | 1,574 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,395 software developers and data experts.

Calling mamaged C# code from Visual C

Hi,
I've got a legacy application written in C. I'd like to evolve the product
(as opposed to completely rewriting it) by adding new features using C#. Is
this possible? I know I should be able to call a managed-code DLL from C,
but I am unsure of the best way to do it, and also how I might marshal the
data between the two parts between the managed and unmanaged parts of the
application.
Can anyone point me in a direction?
Thanks.
Nov 16 '05 #1
5 1273
We had a similar problem. We wanted ActiveX code written in Visual Studio 6
to communicate with a WebService in .NET.

The way we solved it was to create a Managed C++ dll that was also a regular
dll. Use the wizards to create a regular dll, then enable the support for
managed extensions.

The VS6 code calls functions in this dll which in turn calls the WebService.
The limitation of using a regular dll is that you can only use C types, no
CStrings. Since your code is written in C, this should not be a problem.

When implementing this we had a problem freeing memory allocated within the
DLL. From what I could understand, the runtime tracks which CRT the memory
was allocated on. In our case I think the managed DLL used VS.NET version
and the ActiveX used VS6.
In order to overcome this, we created a method called FreeMemory() in the
DLL, this frees the memory in the context of the DLL.
Chris
"Tim Nelson" <ti***************@softhome.net> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
Hi,
I've got a legacy application written in C. I'd like to evolve the product (as opposed to completely rewriting it) by adding new features using C#. Is this possible? I know I should be able to call a managed-code DLL from C,
but I am unsure of the best way to do it, and also how I might marshal the
data between the two parts between the managed and unmanaged parts of the
application.
Can anyone point me in a direction?
Thanks.

Nov 16 '05 #2
Tim,

You have a few options here. If your code doesn't do anything too
tricky, you might be able to just add the /clr switch to the compiler
process and then you will automatically be able to access managed code.

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

"Tim Nelson" <ti***************@softhome.net> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
Hi,
I've got a legacy application written in C. I'd like to evolve the product (as opposed to completely rewriting it) by adding new features using C#. Is this possible? I know I should be able to call a managed-code DLL from C,
but I am unsure of the best way to do it, and also how I might marshal the
data between the two parts between the managed and unmanaged parts of the
application.
Can anyone point me in a direction?
Thanks.

Nov 16 '05 #3
Chris,
If I read between the lines here... I think you are saying I can't call a C#
managed DLL directly from C, I have to use a managed regular C++ DLL as a
wrapper, right?
"Christopher Kimbell" <a@b.c> wrote in message
news:40********@news.broadpark.no...
We had a similar problem. We wanted ActiveX code written in Visual Studio 6 to communicate with a WebService in .NET.

The way we solved it was to create a Managed C++ dll that was also a regular dll. Use the wizards to create a regular dll, then enable the support for
managed extensions.

The VS6 code calls functions in this dll which in turn calls the WebService. The limitation of using a regular dll is that you can only use C types, no
CStrings. Since your code is written in C, this should not be a problem.

When implementing this we had a problem freeing memory allocated within the DLL. From what I could understand, the runtime tracks which CRT the memory
was allocated on. In our case I think the managed DLL used VS.NET version
and the ActiveX used VS6.
In order to overcome this, we created a method called FreeMemory() in the
DLL, this frees the memory in the context of the DLL.
Chris
"Tim Nelson" <ti***************@softhome.net> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
Hi,
I've got a legacy application written in C. I'd like to evolve the

product
(as opposed to completely rewriting it) by adding new features using C#.

Is
this possible? I know I should be able to call a managed-code DLL from C, but I am unsure of the best way to do it, and also how I might marshal the data between the two parts between the managed and unmanaged parts of the application.
Can anyone point me in a direction?
Thanks.


Nov 16 '05 #4
C# dll's don't export functions the way C dll's do: they export classes
(which are not available to C).
So you can:
- Compile your C code in Managed C++ (the MC++ compiler understands about
anything the "old" C++ compiler did - I think they only switch the backend)
- Use a Managed C++ wrapper
- implement a COM interface in your C# class, and call that from C

Niki

"Tim Nelson" <ti***************@softhome.net> wrote in
news:OY**************@TK2MSFTNGP10.phx.gbl...
Chris,
If I read between the lines here... I think you are saying I can't call a C# managed DLL directly from C, I have to use a managed regular C++ DLL as a
wrapper, right?
"Christopher Kimbell" <a@b.c> wrote in message
news:40********@news.broadpark.no...
We had a similar problem. We wanted ActiveX code written in Visual Studio
6
to communicate with a WebService in .NET.

The way we solved it was to create a Managed C++ dll that was also a regular
dll. Use the wizards to create a regular dll, then enable the support for managed extensions.

The VS6 code calls functions in this dll which in turn calls the

WebService.
The limitation of using a regular dll is that you can only use C types, no CStrings. Since your code is written in C, this should not be a problem.

When implementing this we had a problem freeing memory allocated within

the
DLL. From what I could understand, the runtime tracks which CRT the memory was allocated on. In our case I think the managed DLL used VS.NET version and the ActiveX used VS6.
In order to overcome this, we created a method called FreeMemory() in the DLL, this frees the memory in the context of the DLL.
Chris
"Tim Nelson" <ti***************@softhome.net> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
Hi,
I've got a legacy application written in C. I'd like to evolve the

product
(as opposed to completely rewriting it) by adding new features using
C#. Is
this possible? I know I should be able to call a managed-code DLL

from C, but I am unsure of the best way to do it, and also how I might marshal the data between the two parts between the managed and unmanaged parts of the application.
Can anyone point me in a direction?
Thanks.



Nov 16 '05 #5
Niki,
If I read between your lines.... I think you would recommened
in order of preference:

1. Compile with /clr
2. Managed C++ Wrapper
3. COM Interface

In that order, right ???

Thanks.

"Niki Estner" <ni*********@cube.net> wrote in message
news:eC*************@tk2msftngp13.phx.gbl...
C# dll's don't export functions the way C dll's do: they export classes
(which are not available to C).
So you can:
- Compile your C code in Managed C++ (the MC++ compiler understands about
anything the "old" C++ compiler did - I think they only switch the backend) - Use a Managed C++ wrapper
- implement a COM interface in your C# class, and call that from C

Niki

"Tim Nelson" <ti***************@softhome.net> wrote in
news:OY**************@TK2MSFTNGP10.phx.gbl...
Chris,
If I read between the lines here... I think you are saying I can't call a
C#
managed DLL directly from C, I have to use a managed regular C++ DLL as a wrapper, right?
"Christopher Kimbell" <a@b.c> wrote in message
news:40********@news.broadpark.no...
We had a similar problem. We wanted ActiveX code written in Visual Studio
6
to communicate with a WebService in .NET.

The way we solved it was to create a Managed C++ dll that was also a regular
dll. Use the wizards to create a regular dll, then enable the support

for managed extensions.

The VS6 code calls functions in this dll which in turn calls the

WebService.
The limitation of using a regular dll is that you can only use C types, no
CStrings. Since your code is written in C, this should not be a
problem.
When implementing this we had a problem freeing memory allocated

within the
DLL. From what I could understand, the runtime tracks which CRT the memory was allocated on. In our case I think the managed DLL used VS.NET version and the ActiveX used VS6.
In order to overcome this, we created a method called FreeMemory() in the DLL, this frees the memory in the context of the DLL.
Chris
"Tim Nelson" <ti***************@softhome.net> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
> Hi,
> I've got a legacy application written in C. I'd like to evolve the
product
> (as opposed to completely rewriting it) by adding new features using C#. Is
> this possible? I know I should be able to call a managed-code DLL

from
C,
> but I am unsure of the best way to do it, and also how I might

marshal the
> data between the two parts between the managed and unmanaged parts
of the
> application.
> Can anyone point me in a direction?
> Thanks.
>
>



Nov 16 '05 #6

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

Similar topics

8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
2
by: Ray J. | last post by:
I have a C++ program written and compiled on Solaris 8 with gcc. With gcc lets me compile fortran code along with the C++ program to be able to call the fortran code as a subroutine. The...
8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
1
by: solita | last post by:
I have created a cab for windows CE using visula studio.Can anyone please help me to create a shortcut of my application in desktop.I was able to create shrtcut only in statrt menu.How to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.