C# VS 2013 how to call programs in DLL [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 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.

Related

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).

What does byte& mean? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
//
string productName;
byte& local1 = (byte&) productName;
//
What is this byte&?
I really don't understand. I got code from my friend but I don't understand what this line wants to tell? Because it gives error or redline in my VS2012. Can anyone explain?
It's making local1 a reference byte type, but that's probably coming from some IL decompilation and not valid C# syntax. That result code will more likely happen if you decompile a IL function with a ref string parameter.
You can't compile that code using a C# compiler. There are many things that are legal in IL but there's no C# equivalent syntax, and that's usually the way it's decompiled back (which doesn't make it compilable) in a best-effort to make it look like C#
It is invalid. The closest syntax is byte? - which adds null support to byte.

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.

Use non-managed code (C) in c# [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'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);
}

.Net how to play audio in the root of the website with System.Windows.Media.MediaPlayer() [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 use this code blow in .NET. It works fine. The problem is that I want this audio to play in the root of the website. What changes should I make for this? Thanks
var sample= new System.Windows.Media.MediaPlayer();
sample.Open(new System.Uri( #"D:\voices\1.wav");
sample.Play();
In a web application, this might look something like this:
sample.Open(new System.Uri(Server.MapPath("~/") + #"\voices\1.wav");
I say might because that all depends on whether or not the voices folder exists in the root of the website. Additionally, you should probably leverage Path.Combine instead:
var path = Path.Combine(Server.MapPath("~/"), "voices", "1.wav");
sample.Open(path);
Finally, I don't know what sample is, but the Open method may not work in a website. I'm making the assumption you know what Open does and whether or not it can work in a website.
Use server.mappath("~/") <- that's the filesystem root for your website.
dim path as string = server.mappath("~/") & "/voices/1.wav"
Note that backslashes, for filesystem path, not URI.
Hope it helps.

Categories