include a file
-
Hi, I am working on a c# app that talks to an external device via a comm port. The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's. Is there a way to get thease #define's in to my c# app, other than copying and pasting? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
-
Hi, I am working on a c# app that talks to an external device via a comm port. The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's. Is there a way to get thease #define's in to my c# app, other than copying and pasting? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
Ok, even if i get the header included some how, its still not going to work... MSDN: The #define directive cannot be used to declare constant values as is typically done in C and C++. What a crock.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
-
Hi, I am working on a c# app that talks to an external device via a comm port. The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's. Is there a way to get thease #define's in to my c# app, other than copying and pasting? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
I think if the requirement is to call a function from an unmanaged DLL then
DllImportAttribute class
explained here http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^] may be helpful.#define
being a preprocessor directive as explained here http://msdn.microsoft.com/en-us/library/yt3yck0x(v=vs.90).aspx[^] may not be suitable for the specified purpose. -
Hi, I am working on a c# app that talks to an external device via a comm port. The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's. Is there a way to get thease #define's in to my c# app, other than copying and pasting? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
Well... the quick and dirty way would be to pass it through a C pre-processor. However, you may want to limit the scope of what you do that way. Without seeing what's actually in the header file and which parts you need, I'll show a simple example of what can be done: Example.h
# define OPEN 1
define CLOSE 2
define READ 3
define WRITE 4
Example.csi (choose an extension)
namespace Support
{
public static class Command
{
public const int Open = OPEN ;
public const int Close = CLOSE ;
public const int Read = READ ;
public const int Write = WRITE ;
}// or
public enum Command
{
Open = OPEN
,
Close = CLOSE
,
Read = READ
,
Write = WRITE
}
}Pass them through a C pre-processor, this uses the Visual C/C++ one:
cl /nologo /C /EP /P /FIC:Example.h /FiExample.cs Example.csi
Results in: Example.csnamespace Support
{
public static class Command
{
public const int Open = 1 ;
public const int Close = 2 ;
public const int Read = 3 ;
public const int Write = 4 ;
}// or
public enum Command
{
Open = 1
,
Close = 2
,
Read = 3
,
Write = 4
}
}You can then include this file in your project. If needed, you can have a pre-build step that performs the above pre-processing. Notes: 0) The pre-C# file (csi) should not contain any directives 1) You should include a notice that it was generated 2) You could also set the file to read-only 3) Use a partial class if you want this to be part of a larger class so you don't pass the whole thing through the pre-processor See also my Implanting Common Code in Unrelated Classes[^]
-
Ok, even if i get the header included some how, its still not going to work... MSDN: The #define directive cannot be used to declare constant values as is typically done in C and C++. What a crock.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
What's a crock is that in the original C/C++ files, the original programmer made the mistake of depending on a compiler-specific feature to write code. I know full well that everyone used to use #define to declare their constants in C/C++, even when they had a perfectly good "const" keyword, but in doing so, the made the code specific to the C/C++ compiler. Sorry, but don't blame C# for the failings of some other programmer in a previoud language.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
What's a crock is that in the original C/C++ files, the original programmer made the mistake of depending on a compiler-specific feature to write code. I know full well that everyone used to use #define to declare their constants in C/C++, even when they had a perfectly good "const" keyword, but in doing so, the made the code specific to the C/C++ compiler. Sorry, but don't blame C# for the failings of some other programmer in a previoud language.
A guide to posting questions on CodeProject[^]
Dave KreskowiakAt one place where we did C, the "guru" specified that we should use
define
s (rather thanconst
s etc.) as much as possible because they don't use memory. :-D -
At one place where we did C, the "guru" specified that we should use
define
s (rather thanconst
s etc.) as much as possible because they don't use memory. :-DGuess I have a lot code to change. :omg:
BDF I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be. -- BillWoodruff
-
Well... the quick and dirty way would be to pass it through a C pre-processor. However, you may want to limit the scope of what you do that way. Without seeing what's actually in the header file and which parts you need, I'll show a simple example of what can be done: Example.h
# define OPEN 1
define CLOSE 2
define READ 3
define WRITE 4
Example.csi (choose an extension)
namespace Support
{
public static class Command
{
public const int Open = OPEN ;
public const int Close = CLOSE ;
public const int Read = READ ;
public const int Write = WRITE ;
}// or
public enum Command
{
Open = OPEN
,
Close = CLOSE
,
Read = READ
,
Write = WRITE
}
}Pass them through a C pre-processor, this uses the Visual C/C++ one:
cl /nologo /C /EP /P /FIC:Example.h /FiExample.cs Example.csi
Results in: Example.csnamespace Support
{
public static class Command
{
public const int Open = 1 ;
public const int Close = 2 ;
public const int Read = 3 ;
public const int Write = 4 ;
}// or
public enum Command
{
Open = 1
,
Close = 2
,
Read = 3
,
Write = 4
}
}You can then include this file in your project. If needed, you can have a pre-build step that performs the above pre-processing. Notes: 0) The pre-C# file (csi) should not contain any directives 1) You should include a notice that it was generated 2) You could also set the file to read-only 3) Use a partial class if you want this to be part of a larger class so you don't pass the whole thing through the pre-processor See also my Implanting Common Code in Unrelated Classes[^]