Use non-managed code (C) in c# [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working on a project that combines a very large open source project (OSP) written in C and attempting to build a front end in C#. I'm currently compiling the OSP with Visual Studio 2012 Express and generating all the .exe's, .dll's,etc from this project.
How should I perform the integration? (Remember I have full access to the .h/.c files) I attempted using the IJW (It Just Works) method, but it didn't appear to allow me to import the references for my freshly compiled .dll's. Apart from that, I'm not sure how to execute the various functions within C# (apart from sending command line commands to the .exe's, which I would prefer not to do....)
Do I need to compile the OSP with special options/parameters for IJW, or would it require code rewrite?

For DLL written in C, you need to write the equivalent declarations in C# instead of adding reference to that DLL.
This is called PInvoke.
class ABC
{
[DllImport("abc.dll")]
public static extern int FuncX(int x, int y);
}

Related

Record instance in F# is null in C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm working on an assignment for school in which we make a small game using monogame, with the added challenge of working in F#. The game logic is fully immutable F#, and the entrypoint is in C# by use of the Game class in monogame. I have however come across a weird problem concerning Record types in F#. In the logic (F#) I have something along the lines of:
...
module Vectors =
type Vector = {
x : double
y : double
}
let Zero : Vector = {x=0.0; y=0.0}
...
And in C# I have some code accessing Zero:
...
player.vector = Vectors.Zero;
...
Weirdly enough, when I try to now use player.vector, it comes up as null. When debugging Vector.Zero is also null. I've looked around for a few solutions, and it may be some trivial mistake, but I can't seem to find it.
When you build an F# project as an exe, but use it as a library, occasionally some initialisation code doesn't get called.
This can be fixed by changing the project to build as a library.
Solved - F# project was building as a console application.
Changing it to be a class library fixed the issue.
Thanks #JohnPalmer

How to disassemble .NET code right? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to disassemble my C# code and then debug it on assembly language level.
Let's say we have a simple C# method:
var a = 1235;
var b = ++a;
var c = ++b;
Console.WriteLine("test");
Console.ReadKey();
I've found two different ways how to get an assembly code.
The first one is to start C# code debugging in VS and then open Disassembly window. Here we have the following code.
Everything is OK and assembly code is pretty much simple and short but the problem is that the logic of this assembly code differs from the logic of the IL code generated by ildasm.
So here is the second way. We can compile C# code, use ildasm to get the IL code from PE file and then use ilasm to generate PE file back. Now we have the following assembly code.
As you can see this assembly code is more like the IL code but it contains much more instructions and it is more complicated.
AFAIK C# compiles to CIL code and then to an assembly code in both cases. But it seems to be that in the first way it just compiles to an assembly code.
So the question is why the assembly code of the first method differs from the IL code? And why the assembly code of the first method differs from the assembly code of the second method?
The JIT is able to re-order and merge machine instructions as an optomisation, but will try to avoid moving the effects across sequence points provided by the pdb. The compiler generally generates one sequence point per line of code, since you generally step through it one line at a time.
While C# will often generate multiple IL instructions per line of code, ilasm is given each instruction explicitly and so generates more sequence points, leaving less room for JIT optimizations.

Is it possible to make a program that reads its own source code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
What I mean is, could one possibly make a program that does the equivalent of
public class PrintOwnSourceCode
{
public static void Main ( )
{
System.Console.WriteLine([something]);
// prints "public class PrintOwnSourceCode { public static void Main ( ) { ... } }"
}
}
???
And would that be an example of reflection?
Somewhat.
Decompilers can do something similar to this:
I just decompiled a decompiler so I could use it to decompile itself
.NET Decompilers, like [.NET Reflector] (http://www.red-gate.com/products/dotnet-development/reflector/) and dotPeek are capable of reflecting upon a .NET assembly and generating files that resemble the source code. It will not look exactly like the source code because compiling and decompiling is kind of like translating English to French and then back to English--the results are not always guaranteed to be 1:1 as Google Translate can demonstrate. Information, like whitespace, that are for easy reading but not required by the compiler will be lost in the decompilation process. So, your application could decompile itself or invoke an external decompiler to print itself.
Aside
In compiled languages, the compiled code does not have direct access to the source code. (Companies don't typically ship the source code with the compiled code to customers. They only ship the compiled executable.) When it comes to parsed languages, like JavaScript, it's a whole different story. Because the source must be available to the runtime so that it can be parsed and run, the code can always find it's own source file, open it, and print it out.
This was answered here.
The short answer is that you cannot print it via reflection.
If you want to print out the file, then you will need to load in the source file itself (and have the file available).

C# VS 2013 how to call programs in DLL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a Delphi created DLL that has functions I need to call from VS 2013 C3 app.
Not exactly sure how or where to start to accomplish this.
Do I have to include the dll as a reference or import it somehow or both?
And how do I call the program?
The dll is MSA.dll and the method I need to call looks like this:
GetXML(txtPath.Text, txtCabFile.Text, False);
Any 101 basic suggestion appreciated.
Check out something called "P/Invoke." It allows you to call into "native" (i.e. Delphi, C, etc.) DLLs using simple "extern" function definitions.
Here's a website I use as a resource for P/Invoke calls to the Windows API:
http://www.pinvoke.net/
EDIT: Make sure your target on the .NET side is the same as the one you compiled your Delphi library in. When in doubt, its probably x86 if you're on a PC. Thanks to the commenter below who brought this point to my attention. Don't use AnyCPU.
EDIT 2: The extern declaration you would use:
[DllImport("MSA.dll", CharSet = Ansi)]
public extern string GetXML(string firstParam, string secondParam, bool thirdParam);
You can name the parameters whatever you want. I didn't know what to call them so I just gave them names.

Emitting IronPython code in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Basically I have two questions:
1) How to emit or generate a IronPython code (tools, libs) in a C# application. The result of this process should be string consisting of real IronPython code not IL code.
2) How beneficial is the approach above over generating a IronPython code on your own (simply by using StringBuilder)?
I am looking for some code generator library similar to this IMAGINARY pseudo code generator:
IronPythonCodeGenerator generator = new IronPythonCodeGenerator();
Parameter param = new Parameter("str");
ParameterValue value=new ParameterValue(param,"This piece is printed by the generated code!!!");
Function function = IronPythonCodeGenerator.CreateFunction("PrintExtended",param);
function.AppendStatement("print",param);
function.AppendStatement(Statements.Return);
FunctionCall functionCall = new FunctionCall(function,value);
generator.MainBody.Append(function);
generator.MainBody.Append(functionCall);
Console.WriteLine(generator.MainBody.ToString());
, which outputs the IronPython code:
def PrintExtended( str ):
print str;
return;
PrintExtended("This piece is printed by the generated code!!!");
Reflection.Emit is for generating IL code, not for generating high-level language code. So if your target language is IronPython, building it up in a StringBuilder is probably your best bet.
It of course depends on your needs. If all you want to do is just generate code without wanting to change the order of methods, or modify methods after they've been defined etc., just constructing code in a StringBuilder and then compiling it would be the easiest way.

Categories