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