473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

adding #include header file

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 ...
Nov 15 '05 #1
5 28600
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Angel" <none> wrote in message
news:ue**************@TK2MSFTNGP12.phx.gbl...
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 ...

Nov 15 '05 #2

"Angel" <none> wrote in message
news:ue**************@TK2MSFTNGP12.phx.gbl...
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.
Nov 15 '05 #3
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" <nt*************@hotmail.com> wrote in message
news:1c********************@arkansas.net...

"Angel" <none> wrote in message
news:ue**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 15 '05 #4
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" <nt*************@hotmail.com> wrote in message
news:1c********************@arkansas.net...

"Angel" <none> wrote in message
news:ue**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 15 '05 #5
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/de...us/cpref/html/
frlrfSystemRuntimeInteropServicesDllImportAttribut eClassCallingConventionTop
ic.asp

Marshaling Data with Platform Invoke
http://msdn.microsoft.com/library/de...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/de...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.

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Newsgroup - Ann | last post by:
I have a library function like GetOptions( ..., struct net_arch_t netArch, ....) and put the declaration into a regular header file like getopt.h. But this function declaration also needs the...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
6
by: alan | last post by:
Dear all, I have written my own function by C. And my development platform is W2k with VC6.0. Then I also defined a header file to extern declare this function. After that, I include this...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
11
by: Gary Wessle | last post by:
Hi is it right to have a line like #include <path/to/header.hfor a library on my system, in my header file and use some functions provided by this library in the implementation file (file.cpp)...
3
by: Dv | last post by:
I have a API lib (written in C/C++) that can be used by C/C++ project. Now, I'm adding support to C# project. I changed the Lib to DLL. This is easy. However, I have no idea how to deal with the...
14
by: Jess | last post by:
Hello, I was told that if I have a template class or template function, then the definitions must be put into the header file where I put the declarations. On the other hand, it is generally...
11
by: subramanian100in | last post by:
Suppose the following is in Test.h #ifndef TEST_H #define TEST_H #include <iostream> #include <string> using namespace std;
1
by: pralu | last post by:
hi i have made one header file named struct EventLogEntry.h can u tell me how can i add it to my main source code ...in visual c
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.