adding #include header file

A

Angel

Is there some way to add a C-header file into a C# project? The problem is
that this .h file contains several complex structures (over 20) that are
used by some unmanaged code. These functions receive these defined structs
as parms and modify them during execution.
Basically, it'd be something siomple like this (but in C#):

#include <parms.h>

W32_PARM parm;

// ... and work from there...
void main()
{
strcpy (parm.type1, "test");
strcpy (parm.type2, "test2");
...
zProcess (&parm); // dll function call that modifies certain members
of parm
//Print parm.result1 and parm.result2
...
}

Would I have to go through the same process if I did it in another language,
such as VB6 or VB .Net? Or is C# as good a language as any other for an
application that will constantly interact w/ unmanaged code? Since it's a
gui application, C or C++ are out of the question :)

Thanks for all your help. I know I've been a pain in the ...
 
N

Nicholas Paldino [.NET/C# MVP]

Angel,

No, this is not possible. There are very limited pre-processor
directives that the C# compiler acknowledges, include not being one of them.
Also, C is a different language than C#, while there are similarities, they
are not compatable.

Even if you have heavy interop, I still think that C# is a good
language. It is what you have to do outside of the interop where C# can
help you, IMO.

Hope this helps.
 
R

Ralph

Angel said:
Is there some way to add a C-header file into a C# project? The problem is
that this .h file contains several complex structures (over 20) that are
used by some unmanaged code. These functions receive these defined structs
as parms and modify them during execution.
Basically, it'd be something siomple like this (but in C#):

#include <parms.h>

W32_PARM parm;

// ... and work from there...
void main()
{
strcpy (parm.type1, "test");
strcpy (parm.type2, "test2");
...
zProcess (&parm); // dll function call that modifies certain members
of parm
//Print parm.result1 and parm.result2
...
}

Would I have to go through the same process if I did it in another language,
such as VB6 or VB .Net? Or is C# as good a language as any other for an
application that will constantly interact w/ unmanaged code? Since it's a
gui application, C or C++ are out of the question :)

Thanks for all your help. I know I've been a pain in the ...

Create a C++ managed class to wrap your unmanaged C code. You can then use
the resulting classes in your C# application and use them just as you would
a C# class.

It is difficult to give more detailed advice since it isn't clear if you
want to just use the "C" structs, or if you also want to include the library
that uses these structs? You can do something like this...

[warning air code follows...]
extern "C" { #include <parms.h> }
#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;
using namespace System:
namespace MyParms{
[DllImport("parms")]
extern "C" void zProcess(W32_PARM param);
...
public __gc class My_Parms {
void ZParms( CTS Stuff, CTS to, CTS fill, CTS the, CTS structs ) {
W32_PARM junk;
junk.stuff = Stuff;
...
zProcess( junk );
}
};
}
* cts ~= just some common type, insert your own types.
 
A

Angel

I'd like to be able to call the dll functions using as parameter the same
struct that's defined in the *.h file. Since the functions modify the same
struct that I send as parm, I'm worried (among other things) that it won't
recognize (or be able to read) the new struct created in C#.

The dll is part of a Win api but, according to the docs, all the export
functions were built with the "_cdecl" calling convention.

The problem with recreating the structs in the h file is that these files
are all exported from a CD and the CD is updated every month. So everytime a
CD is released I have to compare the original struct with the C# struct. I'd
like to be able to only call the functions from the dll with the original
struct.

Thanks for your reply.

Angel


Ralph said:
Angel said:
Is there some way to add a C-header file into a C# project? The problem is
that this .h file contains several complex structures (over 20) that are
used by some unmanaged code. These functions receive these defined structs
as parms and modify them during execution.
Basically, it'd be something siomple like this (but in C#):

#include <parms.h>

W32_PARM parm;

// ... and work from there...
void main()
{
strcpy (parm.type1, "test");
strcpy (parm.type2, "test2");
...
zProcess (&parm); // dll function call that modifies certain members
of parm
//Print parm.result1 and parm.result2
...
}

Would I have to go through the same process if I did it in another language,
such as VB6 or VB .Net? Or is C# as good a language as any other for an
application that will constantly interact w/ unmanaged code? Since it's a
gui application, C or C++ are out of the question :)

Thanks for all your help. I know I've been a pain in the ...

Create a C++ managed class to wrap your unmanaged C code. You can then use
the resulting classes in your C# application and use them just as you would
a C# class.

It is difficult to give more detailed advice since it isn't clear if you
want to just use the "C" structs, or if you also want to include the library
that uses these structs? You can do something like this...

[warning air code follows...]
extern "C" { #include <parms.h> }
#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;
using namespace System:
namespace MyParms{
[DllImport("parms")]
extern "C" void zProcess(W32_PARM param);
...
public __gc class My_Parms {
void ZParms( CTS Stuff, CTS to, CTS fill, CTS the, CTS structs ) {
W32_PARM junk;
junk.stuff = Stuff;
...
zProcess( junk );
}
};
}
* cts ~= just some common type, insert your own types.
 
A

Angel

The dll also contains the same functions (the names en with STD) with
calling convention "_stdcall". I don't know what these things mean, but
hopefully it'll help you.


Ralph said:
Angel said:
Is there some way to add a C-header file into a C# project? The problem is
that this .h file contains several complex structures (over 20) that are
used by some unmanaged code. These functions receive these defined structs
as parms and modify them during execution.
Basically, it'd be something siomple like this (but in C#):

#include <parms.h>

W32_PARM parm;

// ... and work from there...
void main()
{
strcpy (parm.type1, "test");
strcpy (parm.type2, "test2");
...
zProcess (&parm); // dll function call that modifies certain members
of parm
//Print parm.result1 and parm.result2
...
}

Would I have to go through the same process if I did it in another language,
such as VB6 or VB .Net? Or is C# as good a language as any other for an
application that will constantly interact w/ unmanaged code? Since it's a
gui application, C or C++ are out of the question :)

Thanks for all your help. I know I've been a pain in the ...

Create a C++ managed class to wrap your unmanaged C code. You can then use
the resulting classes in your C# application and use them just as you would
a C# class.

It is difficult to give more detailed advice since it isn't clear if you
want to just use the "C" structs, or if you also want to include the library
that uses these structs? You can do something like this...

[warning air code follows...]
extern "C" { #include <parms.h> }
#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;
using namespace System:
namespace MyParms{
[DllImport("parms")]
extern "C" void zProcess(W32_PARM param);
...
public __gc class My_Parms {
void ZParms( CTS Stuff, CTS to, CTS fill, CTS the, CTS structs ) {
W32_PARM junk;
junk.stuff = Stuff;
...
zProcess( junk );
}
};
}
* cts ~= just some common type, insert your own types.
 
T

Tian Min Huang

Hello Angel,

Thanks for your post. I reviewed your description carefully, and now I'd
like to share the following information with you:

1. As stated by the previous replies, we are not able to include C/C++
header files in a C# application. We should use P/Invoke to call unmanaged
APIs. P/Invoke supports the calling conventions othen than the WinAPI
default's StdCall. In addition, C/C++ structure must also be marshalled to
C# struct. Please refer to the following MSDN articles for detailed
information:

DllImportAttribute.CallingConvention Field
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemRuntimeInteropServicesDllImportAttributeClassCallingConventionTop
ic.asp

Marshaling Data with Platform Invoke
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconmarshalingdatawithplatforminvoke.asp

2. Ralph is correct that we can work around this problem by creating
managed C++ wrapper classes. C++ Managed Extensions allow mixing of managed
and unmanaged code, provide direct access to low-level unmanaged APIs, and
export managed C++ classes to .NET. Please refer to the following
specification for detailed information:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/ht
ml/vcmanagedextensionsspec_16.asp?frame=true

Please feel free to let me know if you have any problems or concerns.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
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

Top