Building a C# dll for an application that wants to call c++

  • Thread starter Thread starter Raj Wall
  • Start date Start date
R

Raj Wall

Hi,

I have an application that allows functionality extension by third party
DLL's. All the specs for building these DLL's are targeted to c++, with
demo.h files containing "extern" statements, etc.

However not really knowing c++ I would rather use C#. Could you point me to
documentation/information on how to use this c++ information to properly
construct my C# routine to connect properly?

Thank-you for your help!!
Regards,
Raj
 
Raj said:
I have an application that allows functionality extension by third party
DLL's. All the specs for building these DLL's are targeted to c++, with
demo.h files containing "extern" statements, etc.

However not really knowing c++ I would rather use C#. Could you point me to
documentation/information on how to use this c++ information to properly
construct my C# routine to connect properly?

Are those DLL's .NET assemblies or Win32 DLL's ?

Arne
 
Raj said:
Hi,

I have an application that allows functionality extension by third
party DLL's. All the specs for building these DLL's are targeted to
c++, with demo.h files containing "extern" statements, etc.

However not really knowing c++ I would rather use C#. Could you point
me to documentation/information on how to use this c++ information to
properly construct my C# routine to connect properly?

Odds are reasonably good that unless the application expects you to provide
extensions as COM DLLs then you won't be able to use C# to build an
extension - at least, not without first using managed C++ (or C++/CLI if
you're using VS 2005) to build a "shim" DLL that adapts your C# class to the
C-based interface that the program requires.

If you can give some examples of the types of function signatures that the
application expects your extension DLL to expose, someone will be able to
give you more specific help on how to proceed.

-cd
 
Arne, Carl, hi,
Thanks for your help.The SDK includes a "Demo.cpp" file that references <windows.h> and two SDK .h files. I have included them below ("Wave59" is the name of the application).
Thanks again for your help!!
Regards,
Raj
The two SDK .h files are as follows:
---
Wave59_SDK.h
-------------------
#ifndef WAVE59_SDK_H
#define WAVE59_SDK_H
struct WAVE59_DATASTRUCT
{
int year,month,day,starttime,endtime;
double t;
double open,high,low,close;
int volume;
int upticks,downticks,equalticks;
bool plotme;
bool ascii_been_here;
};
#endif
----------------------------------
Demo.h
---------------------------------
#ifndef DEMO_H
#define DEMO_H
extern "C" double __declspec(dllexport) average(WAVE59_DATASTRUCT *price_ptr,
int currentptr,int *int_args,int num_int_args,double *double_args,
int num_double_args,char **string_args,int num_string_args);
#endif
-------------------------------
And the demo .cpp file is as follows:
-----------------------------
#include <windows.h>
#include "Wave59_SDK.h"
#include "Demo.h"
extern "C" double __declspec(dllexport) average(WAVE59_DATASTRUCT *price_ptr,
int currentptr,int *int_args,int num_int_args,double *double_args,
int num_double_args,char **string_args,int num_string_args)
{
//don't crash if we've got a bad length parameter
if ((num_int_args<1)||(int_args[0]<1))
return 0;
//too soon to start our average, just return the close
if (currentptr<int_args[0])
return price_ptr[currentptr].close;
//calculate a simple moving average of the closes
double average=0;
for (int i=0; i<int_args[0]; i++)
average+=price_ptr[currentptr-i].close;
average/=(double)int_args[0];
return average;
}

#pragma argsused
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return 1;
}
---------------------
Thanks for any advice or pointers!
Raj
 
Raj -

You're going to need to use C or C++ to make an extension for that app. You
could write a wrapper in managed C++ that adapts a C# class to that
interface, but AFIAK there's no way to make a DLL that exposes that
interface directly in C#.

-cd


Arne, Carl, hi,
Thanks for your help.The SDK includes a "Demo.cpp" file that references
<windows.h> and two SDK .h files. I have included them below ("Wave59" is
the name of the application).
Thanks again for your help!!
Regards,
Raj
 
Carl, hi,

*sigh* thanks--I was afraid of that. I was hoping something existed for c++
interfaces similar in ease of use to the ActiveX interop "drop-in", which
automagically eats a ActiveX and gives you a nice i/f to it.

Regards,
Raj
 
Hi Raj,

So far .NET provides two approaches to interop with unmanaged code.
1. P/Invoke, calling unmanaged C++ legacy DLL from .NET.
2. COM Interop, Calling .NET code from unmanaged COM Client or Calling
unmanaged COM server from .NET.
Interoperating with Unmanaged Code
http://msdn2.microsoft.com/en-us/library/sd10k43k.aspx

But so far .NET did not provide such an approach that make a C# .NET
assembly and export function just as legacy C++ DLL do.

Now a possible approach is to write a mix-mode managed C++ to wrap the C#
dll just as Carl said.

If you have further question about this issue, please feel free to post
here and I am happy to be of assistance.


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Peter said:
Hi Raj,

So far .NET provides two approaches to interop with unmanaged code.
1. P/Invoke, calling unmanaged C++ legacy DLL from .NET.
2. COM Interop, Calling .NET code from unmanaged COM Client or Calling
unmanaged COM server from .NET.
Interoperating with Unmanaged Code
http://msdn2.microsoft.com/en-us/library/sd10k43k.aspx

But so far .NET did not provide such an approach that make a C# .NET
assembly and export function just as legacy C++ DLL do.

Now a possible approach is to write a mix-mode managed C++ to wrap the C#
dll just as Carl said.

If you have further question about this issue, please feel free to post
here and I am happy to be of assistance.


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

I for one would like to have a tutorial on how to access hardware in C#.

C++ was a pain, and C# is not possible.

Please try to address the hardware interface of C#.

Thank You
Donald
 
| Peter Huang [MSFT] wrote:
| > Hi Raj,
| >
| > So far .NET provides two approaches to interop with unmanaged code.
| > 1. P/Invoke, calling unmanaged C++ legacy DLL from .NET.
| > 2. COM Interop, Calling .NET code from unmanaged COM Client or Calling
| > unmanaged COM server from .NET.
| > Interoperating with Unmanaged Code
| > http://msdn2.microsoft.com/en-us/library/sd10k43k.aspx
| >
| > But so far .NET did not provide such an approach that make a C# .NET
| > assembly and export function just as legacy C++ DLL do.
| >
| > Now a possible approach is to write a mix-mode managed C++ to wrap the
C#
| > dll just as Carl said.
| >
| > If you have further question about this issue, please feel free to post
| > here and I am happy to be of assistance.
| >
| >
| > Best regards,
| >
| > Peter Huang
| >
| > Microsoft Online Community Support
| > ==================================================
| > When responding to posts, please "Reply to Group" via your newsreader so
| > that others may learn and benefit from your issue.
| > ==================================================
| > This posting is provided "AS IS" with no warranties, and confers no
rights.
| >
|
| I for one would like to have a tutorial on how to access hardware in C#.
|
A tutorial of what? It's just not possible to use .NET to access the
hardware, at least not directly.
| C++ was a pain, and C# is not possible.
|

No it's not possible, but this has nothing to do with C#, which is after all
just a programming language, it's the framework (CLR and FCL) which is not
suited to directly access hardware (devices), this is the domain of device
driver programming using C and/or C++. C# just like any other .NET language
can only access the hardware via the device driver interface.

Willy.
 
Willy said:
|
| I for one would like to have a tutorial on how to access hardware in C#.
|
A tutorial of what? It's just not possible to use .NET to access the
hardware, at least not directly.

A tutorial on writing device drivers, then accessing those drivers in C#.

| C++ was a pain, and C# is not possible.
|

No it's not possible, but this has nothing to do with C#, which is after all
just a programming language, it's the framework (CLR and FCL) which is not
suited to directly access hardware (devices), this is the domain of device
driver programming using C and/or C++. C# just like any other .NET language
can only access the hardware via the device driver interface.

Does C# have a standard way of accessing device drivers ?

Are there any good links on how this is can be done ??
 
| Willy Denoyette [MVP] wrote:
| > |
| > | I for one would like to have a tutorial on how to access hardware in
C#.
| > |
| > A tutorial of what? It's just not possible to use .NET to access the
| > hardware, at least not directly.
|
| A tutorial on writing device drivers, then accessing those drivers in C#.
|

Grab a copy of the WDK
http://www.microsoft.com/whdc/devtools/ddk/default.mspx and read the Getting
Started with ... chapter. If you really feel the need to start driver
development, you should have a solid knowledge of C and a working knowledge
of Windows OS internals and you should get trained, one great company
specialized in Driver develompment training is OSR - www.osr.com.


|
| > | C++ was a pain, and C# is not possible.
| > |
| >
| > No it's not possible, but this has nothing to do with C#, which is after
all
| > just a programming language, it's the framework (CLR and FCL) which is
not
| > suited to directly access hardware (devices), this is the domain of
device
| > driver programming using C and/or C++. C# just like any other .NET
language
| > can only access the hardware via the device driver interface.
|
| Does C# have a standard way of accessing device drivers ?
|

C# applications like any other application on Windows operating systems
cannot access the device driver export routines directly (except for User
Mode Device drivers on Vista), all device IO requests must go via the Win32
subsystem using API calls like CreateFile and DeviceIoControl. These API's
are exported from Kernel32.dll so you will have to use PInvoke to call them
from C#.


Willy.
 
Willy said:
|
| A tutorial on writing device drivers, then accessing those drivers in C#.
|

Grab a copy of the WDK
http://www.microsoft.com/whdc/devtools/ddk/default.mspx and read the Getting
Started with ... chapter. If you really feel the need to start driver
development, you should have a solid knowledge of C and a working knowledge
of Windows OS internals and you should get trained, one great company
specialized in Driver develompment training is OSR - www.osr.com.

Thank You, I'll get it.

|
| > | C++ was a pain, and C# is not possible.
| > |
| >
| > No it's not possible, but this has nothing to do with C#, which is after
all
| > just a programming language, it's the framework (CLR and FCL) which is
not
| > suited to directly access hardware (devices), this is the domain of
device
| > driver programming using C and/or C++. C# just like any other .NET
language
| > can only access the hardware via the device driver interface.
|
| Does C# have a standard way of accessing device drivers ?
|

C# applications like any other application on Windows operating systems
cannot access the device driver export routines directly (except for User
Mode Device drivers on Vista), all device IO requests must go via the Win32
subsystem using API calls like CreateFile and DeviceIoControl. These API's
are exported from Kernel32.dll so you will have to use PInvoke to call them
from C#.


Willy.

I am glad to see that it can be done, "it'll be a pain as usual".
 
Hi Raj,

Based on my experience, the .NET platform languages (I mean the language
using .NET runtime and IL code) are popular due to its easy using(compared
to C++), but C++ is still alive and keep growing. Because C++ has many
advantages, e.g. the performance.

Also the legacy DLL which export function to working with other DLL/EXE is
not longer working in .NET language. So far to use .NET language built
library in unmanaged code, the COM interop is the main approach. That is
the say, the client needs to be COM client, which should follow the COM
rule to access to the .NET library. Or as we discussed in the previous
posts, use VC++ mix mode to create a wrap.

Also AFAIK, so far most of the Game, especially the 3D gaming it mainly
developed with C++ for performance and many existing 3D engine built in the
past are of C++.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top