473,832 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1291
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.ne t> wrote in message
news:eU******** ******@TK2MSFTN GP12.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.co m

"Tim Nelson" <ti************ ***@softhome.ne t> wrote in message
news:eU******** ******@TK2MSFTN GP12.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?
"Christophe r 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.ne t> wrote in message
news:eU******** ******@TK2MSFTN GP12.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.ne t> wrote in
news:OY******** ******@TK2MSFTN GP10.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?
"Christophe r 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.ne t> wrote in message
news:eU******** ******@TK2MSFTN GP12.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*********@cu be.net> wrote in message
news:eC******** *****@tk2msftng p13.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.ne t> wrote in
news:OY******** ******@TK2MSFTN GP10.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?
"Christophe r 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.ne t> wrote in message
news:eU******** ******@TK2MSFTN GP12.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
17508
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 request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException: The directory name is invalid.
2
2825
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 specifications for this project have changed and I am porting the C++ code to Visual C++. Is there a way to call fortran code from a C++ program compiled with Visual C++?
8
952
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 request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException: The directory name is invalid.
1
2225
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 shortcut in desktop using visual studio for windows CE
0
9794
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
10497
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
10539
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
10212
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
7753
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
6951
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
5623
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...
2
3968
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3077
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.