IntPtr problem

G

Guest

Hi All

I have problem with converting VB.NET code to C#. This code looks like this

Declare Function SendMessage Lib "Coredll" Alias "SendMessageW" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Intege
Declare Function GetFocus Lib "Coredll" () As Intege
Const LVM_SETCOLUMNWIDTH = &H101

Public Shared Sub Autosize_ListView(ByVal lv As ListView
lv.Focus(

Dim col2adjust As Intege

For col2adjust = 0 To lv.Columns.Count -
Call SendMessage(GetFocus(), LVM_SETCOLUMNWIDTH, col2adjust, -2
Nex

End Su

I have problem with calling SendMessage function (cannot convert from int to System.IntPtr). Can enyone help me with translation to c#

Thanks for your help
Very kind regards
Krzysztof Kazmierczak
 
C

Chris Tacke, eMVP

Change:

Declare Function SendMessage Lib "Coredll" Alias "SendMessageW" (ByVal hWnd
As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As
Integer) As Integer

To

Declare Function SendMessage Lib "Coredll" Alias "SendMessageW" (ByVal hWnd
As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As
Integer) As Integer

-Chris

Krzysztof Kazmierczak said:
Hi All!

I have problem with converting VB.NET code to C#. This code looks like this:

Declare Function SendMessage Lib "Coredll" Alias "SendMessageW" (ByVal
hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal
lParam As Integer) As Integer
Declare Function GetFocus Lib "Coredll" () As Integer
Const LVM_SETCOLUMNWIDTH = &H101E

Public Shared Sub Autosize_ListView(ByVal lv As ListView)
lv.Focus()

Dim col2adjust As Integer

For col2adjust = 0 To lv.Columns.Count - 1
Call SendMessage(GetFocus(), LVM_SETCOLUMNWIDTH, col2adjust, -2)
Next

End Sub

I have problem with calling SendMessage function (cannot convert from int
to System.IntPtr). Can enyone help me with translation to c#?
 
G

Guest

Hi Chris!

Thanks, declaration is not a main problem, but I have a problem with calling that function in C#. VB uses GotFocus() function in first parameter of SendMessage, how I should use it in c# version of that code??

Thank you very much
Chris
 
C

Chris Tacke, eMVP

You need to call an API that gets the hWnd of the focused control. It will
likely return an IntPtr, but it could also return an int or a uint - all
depends on how you declare it.

I still am unsure of what language you're trying to do this in. You say C#,
but your code has all been VB.

-Chris



Krzysztof Kazmierczak said:
Hi Chris!

Thanks, declaration is not a main problem, but I have a problem with
calling that function in C#. VB uses GotFocus() function in first parameter
of SendMessage, how I should use it in c# version of that code??
 
G

Guest

Hi Chris

I'm trying to do this in C# (I have an example in VB.NET and I'm trying to convert that code to c#)
I wrote something like this

[DllImport("Coredll", EntryPoint="SendMessageW")
public static extern int SendMessage
IntPtr hWnd
uint Msg
int wParam
int lParam )

[DllImport("Coredll")
public static extern IntPtr GetFocus()

private const int LVM_SETCOLUMNWIDTH = 0x101E

private void ListColumnsAutoSize(ListView lv

lv.Focus()
IntPtr hwnd = GetFocus()
for (int a = 0; a < lv.Columns.Count - 1; a++

SendMessage(hwnd, LVM_SETCOLUMNWIDTH, a, -2)


But this code not work. I don't where's the problem

Kind Regards
Chris
 
C

Chris Tacke, eMVP

Ah, now it's much more clear. On the surface it all looks fine. I'd check
2 things:

1. Make sure the handle returned by GetFocus looks like a valid handle.
The listview must have focus when this is called.
2. Add calls to Marshal.GetLastWin32Error() ater the P/Invoke calls to see
if an error is happening.

-Chris
 

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