Win32 Application in C# .Net - c#

I have developed a window based application based on Native calls. In this application I am handling lots of mouse events, keyboard hooking, painting etc.
My question is that how can I use that win32 application (Unmanaged) in C# .Net?
I have read about the managed wrapper to wrap up win32 application, if you think this is the best option then please suggest me any book or reference of tutorial which can help me to move forward. Otherwise suggest any other option which you think is best in this situation.
Thanks in advance.

at least you can use your .dll through COM Interop.

You have several options if you want to 'control' one application from the other, where one is Native and the other is Managed.
Have you cosidered using named-pipes to 'control' your application?

Related

windows filtering platform. net wrapper?

Is there a .NET wrapper for the windows filtring platfrom? Im looking to use WFP to observe application level network traffic observations in my c# app.
Thanks!
No, I don't believe there is, although lots of people seem to want one. I think you have to fall back to hitting the Win32 API.
I'm writing my own .net wrapper for WFP using P/Invoke Interop Assistant. Here's the thread I used to get it working.
http://social.msdn.microsoft.com/Forums/en-US/wfp/thread/a65bf197-937b-401e-b15f-0e1c3decdb14/

How to get text from unmanaged application to a c# application?

I have a 3rd party application, which creates several windows of which each has a textbox with text I want. I then want to use that information within my application, so I just need to obtain that information (possible trigger some commands back to it later on by pressing buttons)
The 3rd party application is un-managed C++.
My application is C# (.NET 4.0).
I have seen that you can do 'hooks' into other application but I'll be honest I am completely lost of which route to take and how to go about it.
Some advice would be great.
I wonder if all this talk of managed code and unmanaged code and Interop isn't a red herring. Are you able to interface with this other app at all or are you just going to have to use FindWindow to find that other app and then enumerate it's child windows till you find the text boxes you're after, then just call GetWindowText ?
You will need to use COM interop. More here
http://msdn.microsoft.com/en-us/library/aa645736%28v=vs.71%29.aspx
http://www.liensberger.it/web/blog/?p=323
The easiest interop with an unmanaged C++ is via C++/CLI. If there's already a simple C wrapper then P/Invoke will be sufficient.

How to create an in-between library?

I am looking for some information on how to achieve something with libraries in C++ and c#.
What I would like to know is how to approach the following problem:
C# application: -has a window
C++ library: -has a function called create_button(x,y), when
invoked, it will create a button on
the c# application's window. (if the
C# application is not running,
nothing will happen)
C++ application: -dynamicaly links to the C++ library and calls the create_button()
function.
How would I approach this problem, I would be glad to hear some of your ideas.
The platform is windows. My question is, how would I let the C++ library communicate to the c# application to create a new button? Is it linked, sockets, ... I'm particulary thinking of GTK+ in linux, you link to the gtk+ library, but how does the library interface with GNOME to create a new window etc, something like that. I'm not interested in writing dlls and linking those to a c# application, I'm interested in creating an in-between library.
I can't think of any sane way to do what you want to do. What I believe you should be doing is creating functions to do the drawing in the C# app and then exposing some messaging interface, such as a socket, that allows external apps to send messages that command the C# app to do what you tell it. When the C# app receives messages of with message type DRAW_BUTTON, it draws the button, with whatever parameters were specified in the message it received.
You're going to run into problems here since C# is managed and C++ is native. As far as I know, the only way of calling native code from managed in the CLI is by using the P/Invoke layer, in which case you would need to import a DLL, write a prototype, etc.
In addition, I believe that the P/Invoke calls are to C functions, and not C++, although you can get past this by adding a C library to call into which in turn calls your C++ library.
If you can create a small but proper HWND in C# (don't know the widget), you can use that and create a c++ window using the C#'s window as parent.
We did exactly this a few years ago for a java/c++ process pair. However, the Java app could report the HWND value to the c++ app over RPC, so it wasn't that hard to setup.
Given that you control the code of the C# application and you control the code of the C++ application and, presumably, the 'in between library'. I find myself asking "why are you doing it like this?"
If you want the C++ app to be able to cause the addition of a given button on the C# app and for the press of that button to be able to communicate with the C++ app then, personally, I'd use standard IPC and have a communications channel between the C++ app and the C# app and simply have the C++ app ask the C# app, via IPC, to display the button and then have the C# app send whatever detail (most probably the fact that the button was pressed) to the C++ app via the same IPC channel.
If this route wont work then I think you need to clarify the problem that you're trying to solve as at present I think your current 'solution' is on the wrong track and so an answer telling you how to achieve your current 'solution' would be misguided.

Embed non-managed directX into C# form

I got a quick question about running a directX application (C++) in a managed environment. I'm attempting to write a MDI tool (in C#) where the background of the parent window would be an embedded render window of directX (C++).
I've read ways that involved writing the C++ into a native dll, however it would be prefered to be able to have the C++ projects included within the solution (I dont even know if that's possible though). Eitherway, if you know of some helpful steps, hints, or if this is a bad idea in general, please let me know. Thanks!
The easiest way to do this would be to add a C++/CLI project to your solution. This would then enable you to use the DirectX COM interfaces directly and create a managed wrapper that's easy to call from your C# code. I've taken this approach a few times and it's by far the easiest way of mixing DirectX and .Net that I've ever tried. Managed DirectX used to be an option, but it's no longer supported and it was a fairly small subset of the full COM API anyway.
First of all writing the C++ part in a different dll file doesn't mean that it couldn't be at the same solution as the C# project.
In order to use native DX to render on a managed window you need to pass the HWND window id (use form.WindowId.ToInt32) to the C++ D3Ddevice c'tor. after that each time you'll render using that device it would render on the .NET window.
To do this you probably need two saparate projects - a C++ dll & .NET project. use COM wrapper or p-invoke to pass the HWND to the C++ dll.
If you don't want to spend too much time on writing C++ code for DirectX, you can consider using
SlimDX, since Managed DirectX 1.0 is out of the question, where as 2.0 never leaves the beta and later replace with XNA which has quite different from DirectX itself, and require you to install XNA Game Studio
SlimDX is the opensource version of managed directx with slightly different API and internal structure, but it's easy to use. The recently released version is very stable. I'm currently using it to write a production application.
SlimDX
Managed DirectX

C# controls in a MFC Application

I am responsible for the User Interface of an application written completely in Visual C++ using MFC and some third-part controls. I would like to use C# (WinForms or even better WPF) to improve the application look&feel.
I would like some advices about how to do it. Links, articles, examples...
Right now the user interface is isolated in a single project and I don't want to compile the whole module with CLR. So how do I have to manage that from the architectural point of view?
I have already looked at the Internet for the subject and read MSDN information. I would like more detailed information...is it convinient? pros/cons? have you used this approach successfully in a "big" application?
I don't want to compile the whole ui project with CLR...can I just have all the .NET code in a isolated project and call it from the ui project? what's the best way to do it?
Thanks in advance.
A good starting point is the Win32 and WPF interop page on MSDN.
I found this codeproject article gave a good introduction to the subject of mixing mfc / winforms code.
When faced with the same problem, I made an ActiveX control in C# and used it in my MFC app. The folks at MS took out support for building ActiveX controls with .NET, but it's still possible to do so with a plain Jane COM class which has a custom [ComRegisterFunction()] and [ComUnregisterFunction].
Although MS would like to tell us that the /clr flag will solve our problems, it measurably slowed down my large MFC app.

Categories

Resources