I need to create a .lib file from a C# DLL (I think it is C# becuase of this code which calls to the dll https://code.google.com/p/thunder-missile-api/downloads/detail?name=MissileLauncher.cs&can=2&q=) In other word's I need to create a .lib for DreamCheeky Thunder Missile Launcher DLL, which comes with their software .
Now, what I need to do? I need to operate this device using C++. Easiest way is using their own DLL. The above linked code does it in C#.
I tried importing the DLL file into the project C++, but it seems like some methods are missing, specially methods like moveMissileLauncher() which are called in the C# code.
And the best thing is, I might want to move to QT (most probably) so you know, having a .lib is a good idea.
Crating a lib from a managed dll will do no good. You need to use interop
http://msdn.microsoft.com/en-us/library/ms973872.aspx
In your specific case, I would write a C++ lib that exposes the methods you need/want to call and forwards them to the managed C# dll using interop
I would do that in C++/CLI, personally.
A good, more recent article on the options you have is here http://msdn.microsoft.com/en-us/magazine/dd315414.aspx, or look here on SO for COM/.NET interop and you will find plenty of answers.
You want to call managed C# code from your c++ application. Here is tutorial to make someway to call c# code from your c++ application, i have used this method before, and works fine for me.
Related
I have a UI in C# that loads a C++ Dll which contains the code of some process.
So the C# has an interface to communicate with the C++ dll using DllImport.
Now I would like to use the same C# UI but with a different C++ Dll which will have the same function call but totally different process inside.
So basically, in function of some parameters, my UI will load one or the other C++ DLL.
I have tried to create a string variable containing the name of the dll but it doesn't work.
I know I could create a c++ dll which could do the interface between the c++ and the c# code and could load one or the other c++ dll but if it is possible to do it without introducing a new dll, that would be better.
Do you know if there is a way to do that?
Thanks a lot for your help.
I've got a problem which I hope you can help me with.
I created ASP.NET 4.0 web application. I've also got .cpp file generated in some other app. This .cpp file contains functions, which always returns the same number of variables and which always takes the same number of parameters.
What I need to do is being able to use this functions in my web application.
But what is real problem is that I need to be able to replace this functions while running app. What I mean is administrator should be able to login, upload new cpp file, which will replace old functions with new ones. New ones will have the same names, parameters and result number, but will make calculations in a different way.
Is there any way this can be achieved?
Thanks for any help!
MattheW
Precompile the cpp code into dlls and let admin upload dll. Reference dll's from c# app using [DllImport("")] directive.
C++ will need to be compiled in some way or another. You can use a compiled dll written in C++ in your ASP.NET application but the code will still need to be compiled for ASP to be able to use it.
The compiled DLL can then be loaded and unloaded to accommodate changes to the function. You could perhaps even make the ASP.NET server compile the file somehow, but the code still needs to be compileable to a DLL to make it executable.
You need to expose the C++ code via another dll.
The first choice is pinvoke. See:
How to set up a C++ function so that it can be used by p/invoke?
It's also covered here:
http://msdn.microsoft.com/en-us/library/aa446538.aspx
Technically you could also expose via COM or write in managed C++ but those are both overkill if you're just trying to expose a few C++ functions.
I have a xll file which have a function and I want to call that function from C#.
This xll file is used in excel.any body have idea how to do that?.I tried refrencing the xll file but I am not getting the value.If I open excel like start-->programms-->excel and
in excel if i directly give the function I am getting right value.Same thing If i automate excel in c# and opened excel from Microsoft.Office.Interop.Excel and applied the function I am getting error
You probably won't be able to invoke the function directly because an XLL will expect the Excel interfaces to be driving. You'd have to do some serious faking.
Automating it through Excel will work though.
An xll is just a dll. If you know the signature of the function you want to call, you can call it just like any other function. If you don't know the signature but can load the add-in in Excel, there is a way to get Excel to tell you the signature.
Automation only works if you are running Excel. There is no need for that once you know the signature.
There is a few tools you can use:
XLL Plus
XLL Host
The question has also been asked on MSDN Use XLL from C#, this is the answer:
If it is a COM project, you can add it via the COM tab in the Add reference window.
If you need to create a .NET assembly from the dll, you can try to use Type Library Import tool to create a wrapper around the dll:
tlbimp.exe xll.dll /out:xllnet.dll
If the object does not support COM, but only native calls, you will need to platform invoke the methods using the DllImport attribute. See the Platform Invoke tutorial for more information.
I gave all 3 suggestions a go but didn't have any luck. Given the author of the XLL tool Excel-DNA recommends using a tool I suspect consuming a XLL in a Winform/WPF/Console or Web app is not trivial:
https://stackoverflow.com/a/2865023/495455
Okey here's the situation: I've got a microsoft excel macro in vb that I want to call using C#. I've already tried creating a workbook using Microsoft.Office.Interop.Excel, however I don't want to have to run an excel process to run the macro.
So then I thought why not make a vb class library with my code in it so i can still run it and have a clean dll file. It's not needed to keep any sheet related functions since the macro reads a .lua(UTF-8)text file with some advanced regex functions that I just can't get recreated in C#.
Is it possible for me to make the library use interop as well so i can just call the function in my C#? Any examples would be greatly appreciated.
First I assume you are working with VB6 and not VB Script?
I have never worked with VB 6 or earlier, but I think your best be will be to create a COM object and then you can call the COM object from .NET using C#. Here is a quick link I found through BING that I believe will help you get started if this in an option for you.
Walkthrough: Implementing Inheritance with COM Objects (Visual Basic)
I noticed these two statements:
I don't want to have to run an excel process to run the macro
and
the macro reads a .lua(UTF-8)text file with some advanced regex functions that I just can't get recreated in C#
Those two goals are incompatible. The macro relies on excel functions to run. The only way provided by Microsoft to accomplish this is to completely load the Excel app. There is no way to only run the macro.
How can I use the ListView_GetBkImage macro:
http://msdn.microsoft.com/en-us/library/bb761246(v=VS.85).aspx
... from a C#/WinForms application? I think this macro just wraps the SendMessage method, but I'm not sure. I couldn't find any C#-based samples on this.
Basically I'm trying to get a LVBKIMAGE ( http://msdn.microsoft.com/en-us/library/bb774742(v=VS.85).aspx ) structure that references the Desktop's background bitmap.
You cannot. A macro is processed at compile time by the C/C++ compiler, but you want to access the binary library. You will just have to find the macro in the source, see what it does and do the same in your C# code. It shouldn't be anything too complex. Download the Platform SDK if you don't already have it and look in the .h file mentioned in the documentation.
Edit: OK, so the macro is defined as:
#define ListView_GetBkImage(hwnd, plvbki) \
(BOOL)SNDMSG((hwnd), LVM_GETBKIMAGE, 0, (LPARAM)(plvbki))
SNDMSG is simply defined as SendMessage. LVM_GETBKIMAGE is an integer - it's 0x1045 for the ASCII version and 0x108B for the Unicode version. (You probably want the Unicode version if you're unsure.) So the entire thing resolves to:
(BOOL)SendMessage(hwnd, 0x108B, 0, plvbki)
There should be easy enough to map to C#. Look in System.Windows.Forms using Reflector to see how Microsoft have imported the SendMessage function. It will be marked internal, so you cannot call it, but you can copy it. plvbki is a pointer to a struct - you'll need to create a C# equivalent of LVBKIMAGE. Actually, MS have probably done that for you too, so look around for that.