Search:
Namespaces
Discussions
.NET v1.1
Feedback
C# and Header Files
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.framework.interop
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Jeff
Hello,
I would like to give a C# application access to constants defined in
C/C++ header files.
These constants are in header files and look like: #define MAX_LEVEL 1000.
These constants are used by many other applications, and I do not want to
redefine them in my C# code.
I think a managed c++ wrapper is probably the way to go.
Can anyone point me to some code snips and/or articles that would get me
started?
Thanks, Jeff
Reply to this message...
Robert Jordan
Jeff wrote:
[Original message clipped]
A managed C++ wrapper won't help you very much, because C/C++
#defines are just preprocessor macros. You have to write some
wrapper code that redeclares the defines as consts.
(syntax not checked at all!)
class foo {
public:
const int MaxLevel = MAX_LEVEL;
....
}
I'd rather write a script that converts the #defines into C#-Code.
bye
Rob
Reply to this message...
Jeff
Rob,
Good point about the preprocessor issue.
I don't mind redefining them as const. This will still meet my objective.
However, I'm not sure how to handle the marshalling etc.
The script idea is a good one, but I don't think it will work well for me.
Thanks for your help, Jeff
Reply to this message...
Peter Huang (VIP)
Hi Jeff,
What do you mean the marshal issue?
I think what we need to do is just as copy the C++ header definition and
paste into a C# .cs file and then do the replace job to make it an valid
c# code file.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Reply to this message...
Jeff
Hi Peter,
Thanks for your response.
This approach will not work for me because the header files are
used by many other software products/builds. Therefore, I need the
constants & structs etc. to be pulled automatically.
Thanks, Jeff
Reply to this message...
Peter Huang (VIP)
Hi Jeff,
In C# project, we can define the Prebuild event, iin the event, we can run
an command line to do the preprocess of converting the c++ header to c#
header.
Build Events, Common Properties, <Projectname> Property Pages Dialog Box
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/
vcurfcustombuildeventscommonpropertiesprojectnamepropertypagesdialogbox.asp
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Reply to this message...
Jeff
Hi Peter,
Using Managed C++ to reference constants in pre-existing header files
seems to work well for me.
I simply #include the header file that has the #define constants, and then
redefine like: static const int MAX_VALUE = LEGACY_MAX_VALUE;
Thanks, Jeff
Reply to this message...
Peter Huang (VIP)
Hi Jeff,
Thank you for your input.
Yes, managed C++ supported header file preprocess. You will find that the
vc.net will use the cl.exe as the compiler which can be consider as the
extended version of the cl.exe of former vc++ compiler. So it can support
the header file preprocess, while c# use another different compiler csc.exe
which can not handle the c++ header file.
If you still have any concern please feel free to post here.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Reply to this message...
Jeff
Peter,
Thanks for all of your help.
Jeff
Reply to this message...
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com