I want to read the constants in C#, these constants are defined in a .h file for C++. Can anyone tell me how to perform this in C# ? The C++ .h file looks like:
#define MYSTRING1 "6.9.24 (32 bit)"
#define MYSTRING2 "6.8.24 (32 bit)"
I want to read this in C# ?
Here is a really simple answer that you can use on each line of your .h file to extract the string value.
string GetConstVal(string line)
{
string[] lineParts = string.Split(line, ' ');
if (lineParts[0] == "#define")
{
return lineParts[2];
}
return null;
}
So any time it returns null, you don't have a constant. Now keep in mind that it is only getting the value of the constant, not the name, but you can easily modify the code to return that as well via out parameter, etc.
If you want to represent other data types, like integers, etc. you will have to think of something clever since macros in C++ don't really count as type safe.
You have two options:
1- Create a C++ wrapper code, which wraps these macros, export this code to a lib or dll and use it from C#.
2- read/parse the .h file from your code, and get the values at run-time.
Related
I am developing a program using C#, but I just figured out that what I am going to program is very very difficult in C# yet easy in Python. So what I want to do is make a .PYD file and use it in C# program, but I don't know how. I've been searching about it but I couldn't find anything.
So these are my questions: How do I use .PYD file in C#? I know that .PYD files are similar to .DLL files, so are .PYD files still usable in computers that have no Python installed?
A .pyd file IS a DLL but with function init*() where * is the name of your .pyd file. For example, spam.pyd has a function named initspam(). They are not just similar, but the same! They are usable on PCs without Python.
Do not forget: builtins will NOT be available. Search for them in C, add them as an extern in your Cython code and compile!
Use pythonnet
Tutorial
You need a GIL state.
using (Py.GIL())
{
// Your code here
}
Inside the GIL block, create a scope.
using (Py.GIL())
{
using (PyScope scope = Py.CreateScope())
{
// Your code here
}
}
To execute code:
scope.Exec(codeStr);
To set a variable:
scope.Set(name, value); // 'name' is a string and 'value' is a 'PyObject' object.
To convert to PyObject:
obj.ToPython()
Convert to PyObject if you need to put your object into Python.
To evaluate[0][1][2][3][4] an expression:
scope.Eval(expr)
To get a variable[5][6] value:
scope.Get(variableName)
C# code that doesn't have an end semicolon before any comment and is one-line represents an expression.
References
0: https://codereview.stackexchange.com/questions/160524/evaluating-arithmetic-expressions1: https://www.programiz.com/python-programming/methods/built-in/eval2: What does Python's eval() do? on SO3: https://docs.python.org/3/reference/expressions.html4: Python - Evaluate math expression within string on SO5: https://realpython.com/python-variables6: https://www.w3schools.com/python/python_variables.asp
I have been researching a simple way to start a Python (not IronPython) script and pass parameters such as an Array, String, Int, Decimal and then retrieve an output such as an array from the Python script.
All research points to very complicated methods that are unreliable or IronPython which is limited.
If VBA can do this via xlwings (really well), why cant C# within Visual Studio?
Any advise, links or an example would be really helpful. Thank you
As #denfromufa mentioned, Pythonnet is the way to go. Here is how you can start a module and pass objects to it from C#.
Note:
numpy is given here for example but you can replace it with any other module, even a custom one.
an array object is showed as example but it is the exact same thing for string, double, int, etc.
In this example I execute everything directly in the Python engine using PyScope, since your question was from C# to Python. If you need the object on the C# side, you can use dynamic types instead and import the module with Py.Import().
using Python.Runtime;
double[] a = [2.1, 3.5, 5.4];
using (Py.GIL())
{
using (PyScope scope = Py.CreateScope())
{
scope.Exec("import numpy as np");
scope.Set("a", a.ToPython())
scope.Exec("b = np.array(a)");
}
}
I have a binary file written by VB6 application and now would like to use C# application to read the VB6 exported binary file. I have used the Microsoft.VisualBasic.dll into my C# project.
However there are some data inconsistency in C# application but I have check it in VB.net and it works nicely as well. (I convert from VB6 to VB.net, then VB.net to C#)
The screenshot represents the result from reading binary file by using C# and VB.Net application.
VB.Net is my expected result and now my C# application was showing inconsistency result
Both are double values in C# and VB.NET, and based on my observation, the int, string values looks fine.
In C# I was using statement shown as below, BinaryDetails is struct and inside have few double variables
ValueType DetailsValueType = (ValueType)BinaryDetails;
FileSystem.FileOpen(FileNumber, FileName, OpenMode.Binary, OpenAccess.Read);
FileSystem.FileGet(FileNumber, ref DetailsValueType);
I have change the data type in C# from double to float, still not my expected result:
You can reverse-engineer this kind of mishap with a little test program:
class Program {
static void Main(string[] args) {
var value1 = 3.49563395756763E-310;
var bytes1 = BitConverter.GetBytes(value1);
Console.WriteLine(BitConverter.ToString(bytes1));
var value2 = 101.325;
var bytes2 = BitConverter.GetBytes(value2);
Console.WriteLine(BitConverter.ToString(bytes2));
}
}
Output:
CC-CC-CC-54-59-40-00-00
CD-CC-CC-CC-CC-54-59-40
Note how you are definitely on the right track, you are reading correct byte values from the file. Those doubles have CC-54-59-40 in common. It is just that you read the data misaligned. You started reading too late, off by 2 bytes.
That's because your BinaryDetails is not an exact match with the data in the file. Do keep in mind that you have to assume the file contains VB6 data types. They are slightly different from C# types:
VB6 file data is tightly packed, you need [StructLayout(LayoutKind.Sequential, Pack = 1)]
A VB6 Integer is a C# short
A VB6 Long is a C# int
A VB6 Boolean is a C# short with -1 = True, 0 = False;
VB6 strings have a fixed width, you need to read them as byte[]
Ought to be enough to solve the problem. And of course keep in mind that it is very simple to use a VB.NET assembly from a C# program.
I want to write simple macro in c#, to be more clear i just want to save some typing when testing (but want use snippets, cause they don't save space). Want to replace reserved keywords with shorter words
Example in c++ (return sum):
#define r return
int foo(int i, int c){ r (i + c); }
So the point is to save same space and typing, is there something similar in c#?
C# does not support macros like C++. Visual Studio on the other hand has snippets
Check that question:
C# Macro definitions in Preprocessor
Macros in a different sense:
http://alookonthecode.blogspot.com.au/2011/06/macros-in-c.html
A Macro Preprocessor in C#
http://www.codeproject.com/Articles/10049/A-Macro-Preprocessor-in-C
I just want to convert a piece of PHP code to C# so I need it's equivalent.
And what does unpack really do? I'm very interested in this function.
Unpack reads the binary data according to the data type you tell it to parse as and returns the values in an array.
The closest thing I can think to this would be to a struct within C(++) / C#, where it populates the struct's members with information from the binary data. A struct within C style languages is like an object, but without methods.
I can't think of any good examples right now on how to read data into a struct, but that's because I'm not really very good at C or C++ or C# for that matter. Try looking at this for examples on how to read data into structs ... or as always ... google it.
Public Shared Function unpack(str As String) As String
Dim x As Integer
Dim rstStr As String = ""
For x = 0 To str.Length - 1
rstStr &= Convert.ToString(Asc(str.Substring(x, 1)), 16)
Next
Return rstStr
End Function