IS it possible to block message Box from Existing DLL? - c#

I am working with C# winforms Application i am dll in my project when call that function from that dll i get unwanted MessageBox from that .Is it possible to block that MessageBox?

If push comes to shove, you can fire up a thread that kills off any open windows by title using WinAPI or libraries.
I'd resort to less harsh mechanisms like changing the dll first or putting a change req to the right people.

There is no nice option to get rid of the message box if this is a third-party dll.
However, as C# is compiled to IL you can view the byte code and remove the call to MessageBox.Show or replace it with a call to Trace.WriteLine. You can do this e.g. using the ildasm.exe/ilasm.exe tools coming with the SDK.

You can study that dll with hex-editor, debugger or some resource viewer to find out how dialog box is created and then subscribe to that Windows event (Like OnCreate - surf WinAPI docs for info on Windows creation functions). In your event handler try to suppress dialog box and see if dll function is happy about the fact that dialog wasn't shown

If your dll is managed code you can decompile it and remove MessageBox call like 0xA3 suggested. If your dll is native you can use API hooking. Example of WinAPI hooking you can find here. It's in C++, but can be easily translated to C#.

Related

Calling MFC Dialog from C# .Net?

I need to integrate legacy MFC code into my C# .Net application.
To simulate my MFC code for the purpose of simplifying integration, I created a sample MFC Dialog application, with one button and one edit box. It writes Hello World into the edit box, when I click the button.
My goal is to make this callable from my .Net application. I will be happy if my .Net application can pop up the Dialog box, and have it print HelloWorld when I click the button. I have heard that C++/CLI is a good way to do this. If dynamic linking complicates the matter, I am ok with static linking too.
Firstly, I would like to know if this is even possible? I do not understand the process needed to accomplish this.
So far: I enabled the CLR support from Project properties, and created a C# WinForms application. I was able to add the MFC Dialog application as a reference to the C# WInforms application. But I am stuck at finding out how to export the dialog across the interface. Since, I am doing static linkage, do I still have to write dllexports for the dialog class? How do I export all of the dialog class, how will C# find the resource file that describes how the GUI is laid out for the Dialog?
The code for the dialog box is App Wizard generated, I just added an edit box and a button, and added a handler for the button that writes HelloWorld into the edit box.
To get a start, I tried creating another MFC application to see if I could at least call my MFC Dialog from another MFC application. In the consumer application, I added my Hello World Dialog app as a reference, and included the path to the Dialog header file. But it gave me error saying that IDD_DLG, which is the ID for my dialog was unidentified. So, I need to figure out how to supply with the resource file, where all the IDs are defined.
MFC Methods cannot directly be P/Invoked (see this SO Answer), what I have done to invoke a dialog box from another program is to create a DLL containing the dialog box, export a function which can invoke the dialog box and call the DLL function from an external application (e.g.: C#):
#include "stdafx.h"
#include "afxdialogex.h"
#include "MyDlg.h"
extern "C" __declspec (dllexport) BOOL ShowDlg()
{ AFX_MANAGE_STATE(AfxGetStaticModuleState());
CMyDlg dlg;
INT_PTR nResponse = dlg.DoModal();
return (nResponse == IDOK);
}
You could also add parameters to pass to/from the dialog box as required...
The keyword here is COM interoperability: Build a COM server (i.e. an ActiveX control) to call unmanaged code from managed code. This is the best way, in my opinion.

Receive messages of other window

What I'm trying to do: receive messages of other process window (Spy++).
For example: I opened new notepad window: http://i.stack.imgur.com/vNg6h.png
And then I moved the mouse over the notepad window, the windows will send this message to the notepad window (or to the main window child window/s):
WM_MOUSEMOVE xPos=100,yPos=200
I want to receive this message exactly when then mouse event is happening (by event or while loop) (probably while loop with Application.doEvents() and Threading.Thread.Sleep(1)).
What I have tried:
create messages listener by the ManagedSpyLib (if you will import this library you can't compile your project... and if you will succeed to import this library it's not built for this mission)
I also searched about it and this is the most relevant result: http://www.codeproject.com/Articles/3923/InterSpy-An-integrated-Windows-message-trace-and-f
but this is a c++ project and I'm trying to do it in C# or VB.NET.
Good example is better than explanation for me. :)
The hooks that you need for this are WH_CALLWNDPROC and WH_GETMESSAGE. And these require DLL injection. You cannot inject managed code and so you will need to use unmanaged code for the hooking. You can then get your unmanaged injected DLL to communicate back to your C# application, but you will have to concede defeat on your attempts to write the hooking code in C#.
You need to look at the HOOK. That's how SPY++ works.
However, .Net is not very suitable for this, because you have to use a lot of WIN32 API functions.

getting control names of another exe

Hello all i have a rather specific Question and please consider my apologies if this is not a standard stack-overflow question ,
i need to know is it possible to get all control names of another exe e.g.
i have a c# application and i want to get all control names of another standalone application running on my computer say b.exe so if b.exe have a textbox and a button in it i want to have names of textbox and button in my c# exe and also i want to have a listener for click events of b.exe
till now whatever i have researched is nothing special actually i dont want full code snaps i just want a guideline can i do it thorugh pinvoke? or winapi? please explain what is pinvoke and winapi and is it possible through application hooking ? once again i need a guideline a way to follow so please help me out regarding this, i know i can get active windows name through pinvoke or winapi but my requirements are little high
1 . Access to control names:
Unfortunately I do not .NET (C#) but under Visual C++ this would be impossible.
The reason is: The controls do not have names in the executable any longer but the names are simply converted to numbers.
Example "myTextControl" would be 1234 in the executable. The information about the (original) name of the control gets completely lost while compiling.
2 . Accessing controls in other executables:
I think under .NET (C#) it is not as easy as under native programming languages (directly accessing the Windows API) but it is possible to access controls of other executables as long as you know the number (e.g. 1234).
Therefore you'll have to find out the window handle of the dialog window containing the control and then you can send messages to the control. Unfortunately many messages containing pointers will require some very tricky hacks. Messages without pointer work well.
3 . Creating a listener for a control in other executables:
Creating a listener (e.g. a click listener) on a control in another EXE is possible however very tricky: This would require writing a native (this means: not .NET / C#) DLL file which is then combined with the Windows API function "SetWindowsHookEx".
I do not think that a .NET API (required for C#) already exists that will do this because there are only few use cases for this.

Read a non C# Apps textbox using windows API from a C# Windows forms App

We have an old Windows 32 bit app written in C++ which does some stuff and displays the results in what resembles a textbox.
I have been ask to write an application in C# that reads the data out of the old app and then further process the data.
The issue is how do I read the textbox in the old application?
Someone told me I can get the “handle “of the application using windows API and step through the controls and then read each ones data! Is this true and if so how would I do it from C#?
This is to be a .Net 4 Windows forms Application.
Many thanks
You're probably going to have to use some Interop calls to accomplish this, specifically using a combination of FindWindow / FindWindowEx and SendMessage & WM_GETTEXT / WM_GETTEXTLENGTH.
Here is an article on the subject (in c++, however the same concepts will just need to be ported to use P/Invoke), it's a bit dated but I believe is should be very relevant to your situation.
You can use Spy++ application (comes with VisualStudio) to inspect your native application and find class name of control you are looking for.
Having that, you can get your native application's main window handle, which is easy if you are responsible for launching that application from your c# app:
...
var proc = Process.Start();
var mainWndHandle = proc.MainWindowHandle;
Otherwise, you will have to find other means of finding that window, fe. you can use function that I've described below to look for that window on your desktop (see msdn page for more info)
If you have all that, you can then get handle to the textbox control, using FindWindowEx function (as well as you can use it to find main window, by passing NULL as a hwndParent).
And when you have handle to this textbox, you can read it contents, by sending message of WM_GETTEXT.
If you haven't been PInvoke'ing much, you can check http://pinvoke.net/ for reference on how to call WINAPI function from your c# program.
Do you really need the UI of the C++ application? Because it will be very unconfortable to have 2 separate UIs with different message pumps. You'll need to syncronize this mess on the update of the value.
Why don't you properly strip the logic code from the C++ application and expose it in a Interop CLR project (C++/CLI)? If you don't need to add more features to the C++ app, it seems very straightforward to me

Prevent C# Dll from being loaded for certain apps

Greetings all,
I have a shell toolbar extension written in C#. It's only meant to be used in Windows Explorer, so I want to prevent the DLL from being loaded in Internet Explorer. Windows provides tons of ways to load extensions in IE only, but seemingly no way to do Explorer only. I know there are various checks I could perform in different places after the DLL is loaded, but the ideal would be to prevent the DLL from loading at all.
Now, if it were written C++, I would call GetModuleFileName in DllMain, check if the executable was iexplore.exe, and return false on attach if so. But there is no DllMain in C#; Microsoft doesn't trust us to play nice with loader lock. Is there any other way I can selectively prevent a C# DLL from loading?
Don't do Shell Extension Handlers in .NET
http://blogs.msdn.com/b/junfeng/archive/2005/11/18/494572.aspx
Section 3: Registering our AppBar
http://www.codeproject.com/KB/shell/csdoesshell3.aspx?print=true
I had to send this one in a separate post as a new user is not allowed to send more than one link in 1 post.

Categories