Conversion help, please

O

O-('' Q)

Hi all.

I am looking for help in converting this delphi code to C#.net form. I
am having trouble getting it right as I am still learning (I know this
may be advanced for me in c#) right now. What this code does is it
searched, with a wild card, for a window mathing the string passed to
the function and then returns the hWnd to the calling function.

Any help converting this is much appreciated. I am using Visual Studio
2005 C#.NET if this helps any.

[CODE BEGIN]
function HWndGet(partialTitle: string): hWnd;
var
hWndTemp: hWnd;
iLenText: Integer;
cTitletemp: array [0..254] of Char;
sTitleTemp: string;
begin
//find first window and loop through all subsequent windows in the
master window list
hWndTemp := FindWindow(nil, nil);

while hWndTemp <> 0 do
begin //retrieve caption text from current window
iLenText := GetWindowText(hWndTemp, cTitletemp, 255);
sTitleTemp := cTitletemp;

//frmMain.ListBox1.Items.Add(cTitletemp);

//clean up the return string, preparing for case insensitive
comparison
sTitleTemp := UpperCase(copy( sTitleTemp, 1, iLenText));

//use appropriate method to determine if the current window's
caption
//either starts with or contains passed string
partialTitle := UpperCase( partialTitle );
if pos( partialTitle, sTitleTemp ) <> 0 then
break;
//get next window in master window list and continue
hWndTemp := GetWindow(hWndTemp, GW_HWNDNEXT);
end;
result := hWndTemp;
end;
[CODE END]

-- Kirby
 
F

Fitim Skenderi

Hi,

try the class below:

public class SomeClass
{
public const uint GW_HWNDNEXT = 2;

[DllImport("User32.dll")]
public static extern int GetWindowText(int hWnd, StringBuilder
lpString, int nMaxCount);

[DllImport("User32.dll")]
public static extern int FindWindow(string lpClassName, string
lpWindowName);

[DllImport("User32.dll")]
public static extern int GetWindow(int hWnd, uint uCmd);


public static int HwndGet(string partialTitle)
{
int hWndTemp;
int iLenText;
StringBuilder cTitleTemp = new StringBuilder(255);
string sTitleTemp;

hWndTemp = FindWindow(null, null);

while (hWndTemp != 0)
{
iLenText = GetWindowText(hWndTemp, cTitleTemp,
cTitleTemp.Capacity);
sTitleTemp = cTitleTemp.ToString();
sTitleTemp = sTitleTemp.ToUpper();
if (sTitleTemp.Contains(partialTitle) == true)
{
// IntPtr hWnd = new IntPtr(hWndTemp);
// return hWnd; - if you want to return IntPtr
return hWndTemp;
}

hWndTemp = GetWindow(hWndTemp, GW_HWNDNEXT);
}

return 0; // does not find the window
}
}

hope this helps

Fitim Skenderi
 
O

O-('' Q)

Thank you very much for this example. You've been a great help.

Thank you again.

-- Kirby
 
O

O-('' Q)

Hmm... one thing.

I did try this, but it constantly returns zero for some reason.

....
this.txtCaption.Text = "Handle: " + wHandle.ToString();

if (wHandle != 0) {
MessageBox.Show("Running");
Instances++;
}

I guess I jumped into the deep end on this one, hehe. Thanks again for
any further help with this problem.

-- Kirby
 
F

Fitim Skenderi

Hi Kirby,

I don't know whether you have noticed or not, but the function HwndGet() as
a parameter has to take upper case name (it was in your delphi app).

If you want to remove it, then comment the line

sTitleTemp = sTitleTemp.ToUpper();

in the HwndGet() function


Fitim Skenderi
 
O

O-('' Q)

Haha... I am so embarrassed over this one. So simple, yet I didn't see
it.

Thank you VERY much, it works now.

-- Kirby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top