Creating win32api(dll) that will be callable on another language [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can I create win32api(dll) that will be callable on another language? Example:
When I'm using ruby, I should use my dll which written on C# or C++. So how can I create a dynamic library like that?

There are important differences between C++ and C# Dlls.
The C++ ones allow you to expose global functions (using __declspec(dllexport)) that you can call from ruby.
C# Dlls contain a manifest that can be used to find all the types and use the entire object model from said Dll.
I have never tried the later (i.e. all types and class hierarchy) using ruby, but, if possible, it would be a preferred approach to being able to call only the exported functions.
IronRuby apparently has no issues importing .NET types.
To create a C++ library select Visual C++ Win32 project, and then press Next on ensuing dialog to check DLL checkbox. Otherwise select Visual C# Class Library project.

You have two methods.
If your DLL is implemented using native language, such as C/C++. You can export C Function from your DLL and call it on other language. Most languages are able to call C Function either directly or using language extension, such as Win32API in Ruby.
Use COM (Component Object Model). COM is designed to work across languages. This is preferred method because it has many advantages. You can implement COM Class using C#, C++, Delphi and other languages and use it in other language like normal object in that language.

Related

Using a C library in .NET web application [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 8 years ago.
Improve this question
I have the source code for a C library that I need to use with my C# ASP.Net application. It sounds like the way to handle this would be to create a C++ dll wrapper and DLLImport that into my C# project. I have found this to be kind of ugly as far as when it throws an exception the entire application crashes. Is this the best way to accomplish this, or is there a safer way? I am trying to do some research on how I might be able to make a COM object with the C library, but haven't really found much there. Is it possible to make a COM object and reference it as .NET managed code?
I googled my way around this. I found this great article comparing
a C# facade using with PInvoke and a lot of marshalling
a Facade into the interop layer in C++/CLI and compiling in mixed mode for consumption by C#.
Also, this MSDN citation : C++ Interop is recommended over explicit PInvoke because it provides better type safety, is typically less tedious to implement, is more forgiving if the unmanaged API is modified, and makes performance enhancements possible that are not possible with explicit PInvoke.
Also your idea(C++ COM facade) seems legit as preached by the good book with 2 advantages:
The resulting classes can be used from languages other than Visual C++.
The details of the COM interface can be hidden from the managed client code. .NET data types can be used in place of native types, and the details of data marshaling can be performed transparently inside the custom runtime callable wrappers.

Use .NET dll (class library C#) in 4GL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to use a .NET class library written in C# in the 4GL Progress OpenEdge but I did not find good, working example. Progress says it's possible to do what I want to do, but it doesn't work.
Depending on version of Progress it might be possible to call the .Net class natively in the 4GL. However there are some limits to this. Progress for instance is very single threaded and cannot handle multi threaded .Net classes (for instance if an event occurs in a second thread). Basic support was added somewhere around 10.2B but has later been increased.
You can look at these (large) documents:
GUI for .NET Primer
GUI for .NET Programming
They are mostly about GUI programming but can be used as a reference.
The first thing you need to do is set up an assemblies.xml-file to add your component/class to your environment. After that it depends on your specific class but basic OOPABL is "quite" straightforward.
DEFINE VARIABLE c AS CLASS System.Collections.ArrayList.
c = NEW System.Collections.ArrayList().
The progress knowledgebase states:
It is possible to call .Net assemblies (DLLs) from the 4GL by using
COM (ActiveX) wrappers between the 4GL and the .Net assemblies. It is
your responsibility to write the COM (ActiveX) wrapper program. Help
in implementing this is outside of the scope of Technical Support and
we strongly recommend that you utilize the resources Microsoft has
available (msdn.microsoft.com) to help you.
This article might help: Calling a .NET Component from a COM Component
Good luck!

C# and C++ code in same .NET application? [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 9 years ago.
Improve this question
I'm supposed to work with Open CV, which can be programmed in C++ and deploy it in a .NET graphic interface application. I'd really like to work with C # for the interface traits.
What's the best approach for this?
I know of two ways that C# and C++ can be used in the same application.
The simplest way works where the components to be written in C++ and C# can be separated such that one component relies on the other, but they are not mutually dependent. In this case just create two assemblies, one in C# and one in VC++, and reference one from the other. This is the simplest way to do it, not least because it is supported in the UI of Visual Studio.
However, that approach will not work if there is a mutual dependency, ie, class A needs to know about class B and class B needs to know about class A, where class A is to be written in C++ and class B is to be written in C#. It is still possible to write the classes in different languages like that, using a lesser known feature of .NET called multi-file assemblies, or netmodules.
See How to: Build a Multifile Assembly and Multifile Assemblies for instructions. It is useful to remember that the C++ compiler is generally more clever than the others. I seem to remember the procedure was to get C# to compile its half to a NetModule, then pass that to the C++ compiler and linker which was capable of linking it to the C++ parts and creating the final assembly.
An alternative approach, if you only intend to write a small amount of C# code, would be to learn the VC++ syntax for the .NET features you want to use and avoid C# altogether. VC++ can declare managed interfaces and types just like C# can, and if you will not be writing much actual code in C# then this might be easier.

Create C# bindings for complex system of C++ classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have existing C++ lib containing many different classes working together. Some example usage should include something like passing an instance of one class to constructor/method of another class.
I am planning to provide a C# binding for these C++ classes using C++/CLI therefore I don't have to port the whole C++ code.
I can already do this in "Facade" way by creating another class which hides all the classes used in existing C++ code from the user. However, what I want is to provide the same classes with same method signatures to the user.
Is there any guideline or recommendation for this?
ps. I have looked at some of the existing opensource C# to C++ bindings projects. But they seem to used many different ways of doing this, and I don't really understand it.
A lot of this is going to depend on the factoring of your classes.
In the work that I do, I try to treat the C++ classes I model as hidden implementation details that I wrap into appropriate C++/CLI classes. For the most part, I can get away with that by having managed interfaces that are NOT particularly granular. When your implementation involves directly implementing every detail of the underlying C++ code, then you'll end up with a very "chatty" interface that will involve a fair amount of cost in managed/unmanaged transitions.
In particular, if your unmanaged C++ classes use stl, especially stl collection types, you will likely find yourself in for an unpleasant surprise when you discover that every iteration through your stl collections involves several managed/unmanaged transitions. I had an image decoder that used stl heavily and it ran like a dog because of that. The obvious fix to put #pragmas around the code that accessed stl types didn't help. What I found that did work was to hide all of that in a handle-based C interface that hid all the C++-isms behind an iron curtain. No stl exposed anywhere meant that it was allowed to exist as unmanaged code.
I think your biggest issue is going to be in how you handle collections (if you use any) as the C++ collection philosophy and the .NET collection philosophy don't match up well. I suspect that you will spend a lot of time mapping .NET collections of adapted classes to your C++ collections of classes/types.
EDIT
Here's a blog article I wrote about this issue some time ago. It uses the managed C++ dialect, not C++/CLI, but the issue is the same.
I once did a C++ binding with C# using only [DllImport] attribute. If you don'd have any of the STL issues out fried up here says, and your lib is simple enough (as a single DLL, for example), I guess it's the easiest way to bind C++ and C#.
Simple example on MSDN: http://msdn.microsoft.com/en-us/library/aa984739(VS.71).aspx

Using Java-classes with C# [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 3 years ago.
Improve this question
I have a project written in Java (>1.5).
Is it possible to write parts of the project with C#?
For instance the GUI and calling the methods and instantiate the classes written in java?
If yes, how?
I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.
Not without something like ikvm - or using web services etc to communicate between the two sides. Basically it's likely to be much more work than either rewriting your existing project code in C# or writing the GUI in Java.
There is something called Java Language Conversion Assistant for .NET. You can convert your Java classes to c# and start coding.
There is also something called JNBridge (not free).
It seems like my solution is very limited. and apply only to specific version of java.
I probably will stay with old good C :) Can't imagine how to work without shared libraries :)
This document explain how to create a dll from java and use it in C code. I'm not C# or java expert but i'm sure that you can load external dll's in C# as well. So not a complete solution but good starting point, IMHO.
Generally dll it's a perfect way to mixing languages.
In simple way you can pack your java classes to jar file then
In C# use Process class for execute and map IO stream
I did some research on this a few years ago (2005 I believe) and I liked JNBridgePro as the best third party product to do this. Check it out here http://www.jnbridge.com/
Good luck!

Categories