Cannot define/undefine preprocessor symbols after first token in file - c#

how I can use #define on c#??
I have this code on c++
#define _DSC_CHANNEL_ERRORS_HPP_
#define DSC_ChannelErrors(value) (DSC_E01_##value)
this can be variable, how I can use it?
all code is on code

C# does not support preprocessor symbols like C++.
You have to define them like this:
public const int NUM = 1;
If you want to create a macro your best option is something like this:
public static readonly Func<T> Macro() {};

Related

How to access constant values in an unmanaged dll

I have an unmanaged dll, inside which there is a constant value as the following:
#define ProtocolVersion 1
How can I access this value in C#?
p.s.: I checked this post but it didn't work.
This is a preprocessor define and will not be compiled to the DLL. You can however create a C/C++ function returning this value and call it from C#. Something like:
extern "C" __declspec(dllexport) int GetProtocolVersion()
{
return ProtocolVersion;
}
and in C#:
[DllImport("MyDll")]
extern int GetProtocolVersion();

How to Call C++ Native Properity from C#

I'm not that good in C++
but,..
For example I have this
#define GETSomthing_API __declspec(dllexport)
extern GETSomthing_API int nGetSomthing;
And the method I want to import like this
GETSomthing_API int GetSomthing(const char* szConfigPath, char *A, int B)
How Can I call this from C# ?
Beside,
I think my problem is with the parameter type (const char* ) in C++ side, what is the equal type in C# for it! const char*
Thanks,
How to call C++ code from C#
or
Calling C++ function from C#
Using: [DllImport("xxx.dll")] xxx.dll is compile by C++
Hope this help.
There are a couple of ways for calling into native code from C#. The easiest is probably to use P/Invoke.
Say your function is like :
extern "C" int nGetSomeThing(void);
and it is compiled into a YourDllName.dll file, you can use P/Invoke to directly call into the unmanaged code in the following way from C# code :
public static class interop
{
[DllImport("YourDllName.dll")]
static extern public int nGetSomeThing(void);
}
...
interop.nGetSomething() // call to the native function
Refer : http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx.
If you have a lot of functions with complex signatures, you should probably go for an interop layer in C++/CLI.
Refer : http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes
I just added __cplusplus check and it worked!
#ifdef __cplusplus
extern "C" {
endif
GETSomthing_API char* GetAgentId(const char* szConfigPath, char *szA, int ASize);
ifdef __cplusplus
}

How can I declare constant strings for use in both an unmanaged C++ dll and in a C# application?

Curently I'm passing my const string values up from my C++ into my C# at startup via a callback, but I'm wondering if there's a way of defining them in a C++ header file that I can then also refer to in C#.
I already do this with enums as they are easy.
I include a file in both my C++ library project (via a .h file with a pragma once at the top), and my C# application (as a link):
#if _NET
public
#endif
enum ETestData
{
First,
Second
};
I know it sounds messy, but it works :)
But...how can I do the same with string constants - I'm initially thinking the syntax is too different between the platforms, but maybe there's a way?
Using clever syntax involving #if _NET, #defines etc?
Using resource files?
Using a C++/CLI library?
Any ideas?
A C# string constant would take the form:
public const string MyString = "Hello, world";
I think the preferred way in C++ is:
const std::string MyString ="Hello, world";
string in C# is just an alias for the .NET type, String. One way to do this would be make a C++ #define:
#define String const std::string
And your common code would look like this:
// at the beginning of the file
#if !_NET
#define String const std::string
#endif
// For each string definition
#if _NET
public const
#endif
String MyString = "Hello, world";
I have to admit that I haven't tried it, but it looks like it'll work.
Call me funny, but I think the best way to do this is using C++/CLI and C++.
This lets you #include the same strings into two different contexts and let the compiler do the magic.
This will give you arrays of strings
// some header file
L"string1",
L"string2",
L"string3",
// some C++ file
static wchar_t*[] string = {
#include "someheaderfile.h"
};
// in some C++/CLI file
array<String^>^ myArray = gcnew array<String^> {
#include "someheaderfile.h"
};
otherwise you can use the C preprocessor straight out:
// in somedefineset
#define SOME_STRING_LITERAL L"whatever"
// in some C++ file
#include "somedefineset.h"
const wchar_t *kSomeStringLiteral = SOME_STRING_LITERAL
// in some C++/CLI file
literal String ^kSomeStringLiteral = SOME_STRING_LITERAL;

How to define constants in Visual C# like #define in C?

In C you can define constants like this
#define NUMBER 9
so that wherever NUMBER appears in the program it is replaced with 9. But Visual C# doesn't do this. How is it done?
public const int NUMBER = 9;
You'd need to put it in a class somewhere, and the usage would be ClassName.NUMBER
static class Constants
{
public const int MIN_LENGTH = 5;
public const int MIN_WIDTH = 5;
public const int MIN_HEIGHT = 6;
}
// elsewhere
public CBox()
{
length = Constants.MIN_LENGTH;
width = Constants.MIN_WIDTH;
height = Constants.MIN_HEIGHT;
}
You can't do this in C#. Use a const int instead.
Check How to: Define Constants in C# on MSDN:
In C# the #define preprocessor
directive cannot be used to define
constants in the way that is typically
used in C and C++.
in c language: #define (e.g. #define counter 100)
in assembly language: equ (e.g. counter equ 100)
in c# language: according to msdn refrence:
You use #define to define a symbol. When you use the symbol as the expression that's passed to the #if directive, the expression will evaluate to true, as the following example shows:
# define DEBUG
The #define directive cannot be used to declare constant values as is typically done in C and C++. Constants in C# are best defined as static members of a class or struct. If you have several such constants, consider creating a separate "Constants" class to hold them.
In C#, per MSDN library, we have the "const" keyword that does the work of the "#define" keyword in other languages.
"...when the compiler encounters a constant identifier in C# source code (for example, months), it substitutes the literal value directly into the intermediate language (IL) code that it produces."
( https://msdn.microsoft.com/en-us/library/ms173119.aspx )
Initialize constants at time of declaration since there is no changing them.
public const int cMonths = 12;
What is the "Visual C#"? There is no such thing. Just C#, or .NET C# :)
Also, Python's convention for constants CONSTANT_NAME is not very common in C#. We are usually using CamelCase according to MSDN standards, e.g. public const string ExtractedMagicString = "vs2019";
Source: Defining constants in C#

How to export a struct definition from a native C .dll for use in C#

I know how to extern methods in the .dll, how do I extern structs?
I want to create a C method such as
extern __declspec(dllexport) myStructure getStruct();
where myStructure is something like
typedef struct
{
int A;
int B;
char C;
} myStructure;
How can I call getStruct() from a piece of C# code without first defining the same exact struct in C#? I want to keep a centralized declaration of myStructure so that I only have to make code changes in one place.
Thanks a bunch!
What about doing it another way? Define the struct in C# in T4 and then cross gen to the new format in each of the projects:
ie. base.tt
// C# definition of struct
in gencsharp.tt in the C# project
include "base.tt"
... output the C#
in gencplusplus.tt in the CPP project
include "base.tt"
... use C# to gen a cpp .h file
I've done a bit of P/Invoke with C# and I've always had to define structs in C# that correspond to the Win32 struct by definition. The runtime uses the struct definition to marshal the data from unmanaged to managed. Preet's answer is probably the best.

Categories