473,787 Members | 2,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interop question

When executing some win32 messages in c# I get unexpected results.
The following example is suppose to return the handle of an image in a
button control of another application but it return a negative
handle...is anybody know why it return an invalid handle like
"-989523277" ??

IntPtr ImageHandle =
Win32.SendMessa ge(Handle,BM_GE TIMAGE,IntPtr.Z ero,IntPtr.Zero );
MessageBox.Show (ImageHandle.To String());

Note : the third param is set to IMAGE_BITMAP (0); it return 0 if
the button contain no image.

Thanks...
Aug 22 '08 #1
11 2353
On Thu, 21 Aug 2008 20:35:26 -0700, michelqa <mi******@yahoo .cawrote:
When executing some win32 messages in c# I get unexpected results.
The following example is suppose to return the handle of an image in a
button control of another application but it return a negative
handle...is anybody know why it return an invalid handle like
"-989523277" ??
Can you be more specific? Why do you believe the value "-989523277" to be
an invalid handle value? Have you actually tried to use it as an HBITMAP
and had that fail? Can you show a concise-but-complete code sample that
reliably demonstrates whatever problem you're actually having?

Pete
Aug 22 '08 #2
On Aug 21, 11:44*pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Thu, 21 Aug 2008 20:35:26 -0700, michelqa <miche...@yahoo .cawrote:
When executing some win32 messages in c# *I get unexpected results.
The following example is suppose to return the handle of an image in a
button control of another application but it return a negative
handle...is anybody know why it return an invalid handle like
"-989523277" ??

Can you be more specific? *Why do you believe the value "-989523277" tobe *
an invalid handle value? *Have you actually tried to use it as an HBITMAP *
and had that fail? *Can you show a concise-but-complete code sample that *
reliably demonstrates whatever problem you're actually having?

Pete
I suppose it's invalid since it's negative and since i'm not able to
recreate the bitmap in my application...i t return a "Generic
exception"

//Handle = button handle of another application
IntPtr ImageHandle =
Win32.SendMessa ge(Handle,BM_GE TIMAGE,IntPtr.Z ero,IntPtr.Zero );
MessageBox.Show (ImageHandle.To String());
System.Drawing. Bitmap Bitmap =
System.Drawing. Bitmap.FromHbit map(ImageHandle ); //generic exception
from gdi..invalid handle?
// Just want to get a usable bitmap here

Aug 22 '08 #3
Ok , I'll try to explain the problem :
1- I know that WM_GETIMAGE is suppose to return an hImage hex
c50512b3 (3305444019) ... I know that because I'm logging the message
in SPY++
2- My sendMessage in my c# app return -989523277 (or
897592579727799 5699 when modifying sendmessage definition to return a
long instead of a IntPtr) *** Why this difference???? ****
3- Even with the correct image handle hardcoded like this :
System.Drawing. Image MyImg=
System.Drawing. Image.FromHbitm ap((IntPtr)3305 444019);
I get the error : Arithmetic operation resulted in an overflow.

As I understand it FromHbitmap want an IntPtr and 3305444019 is to big
for a Integer

Dont think a more complete example will help to understand the problem
but here is my example...I have a Win32 application and I want to copy
Button image from it and put the image in a button or a picturebox in
my c#

[DllImport("user 32.dll")]
public static extern IntPtr SendMessage(Int Ptr hwnd, int msg, IntPtr
wparam, IntPtr lparam);
int BM_GETIMAGE = 0x00F6;
IntPtr ImageHandle =
SendMessage(Han dle,BM_GETIMAGE ,IntPtr.Zero,In tPtr.Zero);
MessageBox.Show (ImageHandle.To String());
System.Drawing. Image MyImg=
System.Drawing. Image.FromHbitm ap((IntPtr)3305 444019); //Arithmetic
operation resulted in an overflow.
//System.Drawing. Image MyImg=
System.Drawing. Image.FromHbitm ap(ImageHandle) ; //Exception
Button Button1 = new Button();
Button1.Image=M yImg;
Aug 22 '08 #4
On Thu, 21 Aug 2008 22:07:33 -0700, michelqa <mi******@yahoo .cawrote:
Ok , I'll try to explain the problem :
1- I know that WM_GETIMAGE is suppose to return an hImage hex
c50512b3 (3305444019) ... I know that because I'm logging the message
in SPY++
2- My sendMessage in my c# app return -989523277 (or
897592579727799 5699 when modifying sendmessage definition to return a
long instead of a IntPtr) *** Why this difference???? ****
Um. The handle is a 32-bit value. The hex value 0xc50512b3 is _exactly_
the same as the _signed_ decimal value -989523277.
3- Even with the correct image handle hardcoded like this :
System.Drawing. Image MyImg=
System.Drawing. Image.FromHbitm ap((IntPtr)3305 444019);
I get the error : Arithmetic operation resulted in an overflow.
Yes, of course it would. The value 3305444019 won't fit into a _signed_
32-bit integer.
As I understand it FromHbitmap want an IntPtr and 3305444019 is to big
for a Integer

Dont think a more complete example will help to understand the problem
but here is my example...I have a Win32 application and I want to copy
Button image from it and put the image in a button or a picturebox in
my c#
Well, first of all, that example doesn't compile. So it's really not much
of an example. Secondly, it's not a _complete_ example. If it were,
anyone would be able to take the example, run it through the C# compiler
without any changes or additions, and have a working executable that they
could run and see the exact problem.

Anything short of that is not "complete".

That said, in the little bit of code you did post, I don't see anything
that's obviously wrong. If I were you, I would check to see that the
basic concept works as you expect in unmanaged code, that you can get the
basic concept to work using _only_ managed code (i.e. create the Bitmap
standalone, get the HBITMAP from it and use that to create a new Bitmap),
and then verify that the format of the HBITMAP you get from SendMessage()
is compatible with what .NET expects (you can call the unmanaged function
GetObject() to get a BITMAP structure, or GetDIBits() to get a BITMAPINFO
structure).

With such a tiny snippet of sample code, I have to guess, and having to
guess it's my guess that you're getting a valid HBITMAP handle back, but
it represents a GDI bitmap object that .NET just doesn't want to bother
with (most likely because of the format of the bitmap).

Pete
Aug 22 '08 #5
Just to make things correctly :

1- Download controlSpy from
http://www.microsoft.com/downloads/d...displaylang=en
2- Run controlSpy6.exe . Go to the button section, Apply the BS_BITMAP
style, and then go to the Message tab and send the "SetImage" message.
3- With Spy++ get the handle of the button and convert it to integer
4- Replace the Handle value in for the Handle variable in the
Form_load of the following code
5- Run the code. (My code is .net 1.0)
<Code>
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Runtime. InteropServices ;
using System.Diagnost ics;
using System.Data;

namespace GetWin32ButtonI mage
{
public class Form1 : System.Windows. Forms.Form
{
private System.Componen tModel.Containe r components = null;

public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeCompo nent()
{

this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);

}
#endregion

[DllImport("user 32.dll")]
public static extern IntPtr SendMessage(Int Ptr hwnd, int msg,
IntPtr
wparam, IntPtr lparam);

[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
IntPtr Handle=(IntPtr) 666666; //*** REPLACE THE HANDLE HERE ***//
int BM_GETIMAGE = 246;
IntPtr ImageHandle =
SendMessage(Han dle,BM_GETIMAGE ,IntPtr.Zero,In tPtr.Zero);
System.Drawing. Image MyImg=
System.Drawing. Image.FromHbitm ap(ImageHandle) ;
MessageBox.Show ("We have a valid bitmap!!! :
height="+MyImg. Height.ToString ())
}
}
}
</Code>

Now we have a full working example! ;o) You can skip the 3 first step
if you can find a button with BS_BITMAP style and with an image. (not
an icon...BS_ICON)
Aug 22 '08 #6
The ImageHandle you are retrieving is relative to the button's process. In
the process in which you are trying to create the Bitmap that handle is
meaningless.

"michelqa" <mi******@yahoo .cawrote in message
news:e5******** *************** ***********@v57 g2000hse.google groups.com...
When executing some win32 messages in c# I get unexpected results.
The following example is suppose to return the handle of an image in a
button control of another application but it return a negative
handle...is anybody know why it return an invalid handle like
"-989523277" ??

IntPtr ImageHandle =
Win32.SendMessa ge(Handle,BM_GE TIMAGE,IntPtr.Z ero,IntPtr.Zero );
MessageBox.Show (ImageHandle.To String());

Note : the third param is set to IMAGE_BITMAP (0); it return 0 if
the button contain no image.

Thanks...

Aug 22 '08 #7
On Fri, 22 Aug 2008 00:00:19 -0700, michelqa <mi******@yahoo .cawrote:
Just to make things correctly :

1- Download controlSpy from
http://www.microsoft.com/downloads/d...displaylang=en
2- Run controlSpy6.exe . Go to the button section, Apply the BS_BITMAP
style, and then go to the Message tab and send the "SetImage" message.
3- With Spy++ get the handle of the button and convert it to integer
4- Replace the Handle value in for the Handle variable in the
Form_load of the following code
5- Run the code. (My code is .net 1.0)
Okay, I think I see your mistake. Handles are valid only within a given
process. You can't get a handle value from one process, and then use it
in some other process.

Pete
Aug 22 '08 #8
OK thanks....that' s explain the weird unusable image handle... The
*only* way to do it will be by creating and a dll to inject into the
other process and setting a windows hook. Is there any other way to
execute my code into the other process ?

Aug 22 '08 #9
On Fri, 22 Aug 2008 12:08:27 -0700, michelqa <mi******@yahoo .cawrote:
OK thanks....that' s explain the weird unusable image handle... The
*only* way to do it will be by creating and a dll to inject into the
other process and setting a windows hook. Is there any other way to
execute my code into the other process ?
Any way? Sure. But not without your code looking like malware, and not
without using malware-like techniques.

It's _possible_ that if you describe what you're really trying to do here,
from the high-level goal perspective, ignoring completely any particular
implementation details, someone could offer better advice.

Pete
Aug 22 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1011
by: Le Thanh Thuan | last post by:
In my VB.Net project, I using Interop.Excel 11.0 (Excel 2003 I create Setup project in my computer, and then , I setup in another computer using Excel 2000 When I run my application, a error occur File or assembly name Interop.Excel, or one of its dependencies, was not found. File name"Interop.Excel"
1
2583
by: Bob N5 | last post by:
I am working on an application that uses interop to do some simple operations with both Excel and Word. I have most of functionality working, but ran into issues on making things work with both versions of office. My questions are: 1. What is the 'best' practice for building an application that will work with both versions of office? Do I need to have a separate build with different references for each? 2. Can I install interop...
20
3199
by: Razzie | last post by:
Hey all, I'm really going through a small hell right now - I've completely lost it :) I made a project, using two interop libraries from exchange (created them as in this msdn article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsmtps/html/writingmngsinks.asp). I set my project properties as 'Register as COM interop' to true, I can install it my project on my developement machine without a flaw. Great.
3
9456
by: ScottWPNY | last post by:
I am including a reference to ADOX in a C# project by doing the following: In Solution Explorer, right-click the References node and select Add Reference. On the COM tab, select Microsoft ADO Ext. 2.7 for DDL and Security, click Select to add it to the Selected Components, and then click OK. This creates a dll in my debug and release directory named Interop.ADOX.dll.
8
3425
by: Rob Edwards | last post by:
When trying to add the Microsoft CDO for Exchange Management Library (aka CDOEXM.dll) I receive the following message: "A reference to 'Microsoft CDO for Exchange Management Library' could not be added. Converting the type library to a .Net assembly failed. A depended type library 'CDO' could not be converted to a .NET assembly. A dependent type library 'ADODB' could not be converted to a .NET assembly. Item has already been added." ...
1
1974
by: Tom | last post by:
Hi, I have developed a VB.NET app that is referencing and using an old ActiveX component. When I add that reference to .NET, it of course generates an interop file. My question is: If I include the interop file with the application when deploying, do I need the original .dll object? For example, say I am including an old ActiveX component called OldActiveX.dll. .NET will generate an interop.OldActiveX.dll file. When I get ready to...
5
2143
by: Richard Lewis Haggard | last post by:
I am trying to create multi-dimensioned arrays in conventional ASP pages and pass these arrays as arguments to functions that are in a C# interop assembly. ASP complains because it doesn't recognize that the created array is the same as the declared interface argument. The C# function is expecting to receive a 2 dimensioned array and is declared like this: public object InteropFunc( string s1, object arValues ); What should the ASP...
5
1901
by: Yoavo | last post by:
Hi, In my application I have to use som external COM dll's. In the code: using MyDll when I build the project, I get (in addition to the .exe file) a Dll called Interop.MyDll.dll I want to send the .exe to the users without needing to send the dll. Is it possible to build my project without the accompanying dll ?
0
967
by: Ferd Biffle | last post by:
I am trying to convert an Add-In I wrote from .Net 1.1 (32 bit) to 2.0 on 64 bit XP. One of the vendor API Interop dll's that I am referencing is unavailable to my add-in. Looking at the references in the properties in Visual Studio 2005, I see that all of the other Interop dll's I am using are listed as COM, but the dll in question is shown as .Net. I have tried recreating the Interop using both the 32 bit and 64 bit
5
3274
by: John | last post by:
I would like to ask a question that is obvious to all people porting applications from the "traditional" C\VB6 interop scheme choosing C# vs VB.NET. We have a math library in C which ubiquitously takes a float* which represents an array of floats. If the memory is allocated by the client, in C# or VB.NET, I assume it must be "fixed" so that the GC does not move the pointer during the C algorithm. Given that we are choosing between C#...
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.