Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

How do I call C++ functions from a C# coded Service?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chip Gore - 10 Apr 2008 01:43 GMT
Hi -

Using Visual Studio 2005, I have created a stand alone command
line .exe program that "does what I want".

All of the functionality that I would like to make available to my
C#/.NET service is in a few .cpp/.h files (that my .exe main line .cpp
file calls and uses fine).

I'm not sure what I need to do to make my C++ code into something that
can be called from the C# code. I've seen people talk about "P/
Invoke", but all of the documentation that I've found assumes I
have .dll, and I'm not sure what I need to do to change my C++ project
from creating .exe into creating a .dll so I can pick up where the "P/
Invoke" documentation seems to be starting from.

(I know, just a bit "Newby" of me, but that's where I'm at)

I gather that once I have a .dll, I can then import this, and make a
declaration within my C# code to say "this lives over there" and the
rest of the process should flow naturally. (or do I have my hopes set
to high for this?)

Thanks in advance
cg
shashank kadge - 10 Apr 2008 18:22 GMT
In VS2005,
1. create new project, select VC++, select Windows service, so u can
have all the .header file's code into this windows service project.
Build this project.
2. Create new project, select C#, select windows console. Call the
window service created in step 1 from this project.

-
shashank kadge

> Hi -
>
[quoted text clipped - 21 lines]
> Thanks in advance
> cg
Ben Voigt [C++ MVP] - 10 Apr 2008 19:14 GMT
> Hi -
>
[quoted text clipped - 11 lines]
> from creating .exe into creating a .dll so I can pick up where the "P/
> Invoke" documentation seems to be starting from.

You probably don't want p/invoke.  C++ interop is much easier to use and
more efficient.

The big question -- is that same C++ code used by other programs?
If no -- convert it to C++/CLI, by changing "class whatever" to "ref class
whatever", using System::String instead of std::string, and so on.
If yes -- write a ref class wrapper that converts System::String objects
into std::string objects, then calls the existing code.

> (I know, just a bit "Newby" of me, but that's where I'm at)
>
> I gather that once I have a .dll, I can then import this, and make a
> declaration within my C# code to say "this lives over there" and the
> rest of the process should flow naturally. (or do I have my hopes set
> to high for this?)

By using C++/CLI, you will get a true .NET assembly that can be added to
your C# code (whether class library, console application, service, or
whatever) as a reference just like you do with System.Drawing, System.Data,
System.Xml, etc.

> Thanks in advance
> cg

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.