How to make custom marshalling of com to .Net?  
Author Message
zoomer





PostPosted: Common Language Runtime, How to make custom marshalling of com to .Net? Top

Hi,

I am working in vs2005 using vb.net. In my application i am using some of the cominterop to store my settings like Hot key for my application. I am getting an exception when i am reading the stored hot key from my setting object like can not convert the Intptr or UintPtr to integer like this ( casting exception). But i am storing the hot key as integer. But at the time of retrieving the hotkey it's throwing casting exception. Can any body explain how to use custom marshalling in .Net.

Thanks,

Venu




.NET Development18  
 
 
Lepaca





PostPosted: Common Language Runtime, How to make custom marshalling of com to .Net? Top

Do you can give more informations

IntPtr is a pointer. If you have a pointer to a Int32value, you can use Marshal.ReadInt32(<IntPtr>)

If you use an Int32 value, you don't need custom marshaling...


 
 
nobugz





PostPosted: Common Language Runtime, How to make custom marshalling of com to .Net? Top

Are you using the RegisterHotKey() API function Please post that code...


 
 
zoomer





PostPosted: Common Language Runtime, How to make custom marshalling of com to .Net? Top

Hi,

Here is my user32 function declarations.

Private Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Integer, ByVal lpBuffer As String, ByVal nSize As Integer) As Integer

Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer

Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Integer, ByVal id As Integer) As Integer

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer

Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer

Here is my BuildHotKey function

Public Function BuildHotKeyString(ByVal keyCode As Integer, ByVal shift As Integer, ByRef hotKeyString As String) As Integer

Try

Dim sKeyModifier As String

Dim sText As String

If shift And BF_CTRL Then sKeyModifier = "Cntl + "

If shift And BF_ALT Then sKeyModifier = sKeyModifier & "Alt + "

If shift And BF_SHIFT Then sKeyModifier = sKeyModifier & "Shift + "

' Allocate string buffer

sText = Space(cKeyNameBufSize)

Dim MappedCode As Integer

Dim LeftShiftedCode As Integer

Dim LongKeyCode As Integer

Dim StringLen As Integer

LongKeyCode = keyCode

MappedCode = MapVirtualKey(LongKeyCode, 0)

If MappedCode <> 0 Then

LeftShiftedCode = MappedCode * 65536

StringLen = GetKeyNameText(LeftShiftedCode, sText, cKeyNameBufSize)

If StringLen <> 0 Then

BuildHotKeyString = CKEY_VALID

hotKeyString = sKeyModifier & sText

Else

hotKeyString = ""

BuildHotKeyString = CKEY_INVALID

End If

Else

hotKeyString = ""

BuildHotKeyString = CKEY_IGNORE

End If

Catch ex As Exception

msgbox(ex.message)

End Try

End Function

=====================================================================

And here is my Load Hotkey function

Public Sub LoadHotKey(ByRef Hotkey As Integer, ByRef Modifier As Integer)

Try

'-------- Retrieve last used key num & modifier -----------

Dim s As Settings ' Which is a com component object to store the hotkey in the settings file

s = Globals.Settings(CLOC_MODULE_NAME, CSCREEN_CAPTURE_CONTROL_NAME)

Dim v As Object

v = s.Get(CHOTKEY_NUM)

If v = vbEmpty Or v = vbNull Then

' use default

Hotkey = DEFAULT_HOTKEY

Else

Hotkey = v

End If

v = s.Get(CHOTKEY_MODIFIER)

If VarType(v) = vbEmpty Or VarType(v) = vbNull Then

Modifier = DEFAULT_HOTKEY_MODIFIER

Else

Modifier = s.Get(CHOTKEY_MODIFIER)

End If

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

====================================================================

And here is my unregister hotkey function

Public Function UnRegisterTheHotKey()

Try

If UnregisterHotKey(Globals.Application.hWnd, CHOTKEY_ID) Then

hotKeyRegistered = False

Else

#If debugstatements = True Then

MsgBox "RegisterHotKey Failed get last errror " & Error

#End If

End If

Catch ex As Exception

MsgBox("RegisterHotKey Failed get last errror ")

End Try

End Function

============================================================================

And here is my attemptTo register hotkey function

Public Function AttemptToRegisterHotKey(ByVal KeyCode As Integer, ByVal Shift As Integer) As Boolean

Try

Dim err As Integer

If Not hotKeyEnabled Then

MsgBox("HotKey Not Enabled. Aborting.")

AttemptToRegisterHotKey = False

Exit Function

End If

#If debugstatements = True Then

MsgBox("Calling RegisterHotKey " & Shift & " " & KeyCode)

#End If

' attempt to register this hot key

Dim localKeyCode As Integer

Dim localShift As Integer

localKeyCode = KeyCode

localShift = Shift

err = SendMessage(Globals.Application.hWnd, CREG_HOTKEY_MSG, localShift, localKeyCode)

Const ERROR_SUCCESS = 0&

Const ERROR_HOTKEY_ALREADY_REGISTERED = 1409&

Select Case err

Case ERROR_SUCCESS

hotKeyRegistered = True

MsgBox("sucess")

#If debugstatements = True Then

MsgBox("RegisterHotKey OK")

#End If

Case ERROR_HOTKEY_ALREADY_REGISTERED

hotKeyRegistered = False

#If debugstatements = True Then

MsgBox("RegisterHotKey Failed - Select another hot key")

#End If

Case Else

#If debugstatements = True Then

MsgBox("RegisterHotKey Failed get last errror " & err)

#End If

hotKeyRegistered = False

End Select

AttemptToRegisterHotKey = hotKeyRegistered

Catch ex As Exception

Globals.Application.ShowError("Error occured in AttemptToRegisterHotKey method in module ModScreenCapture")

End Try

End Function

I am able to explain like this only . Sorry for this. In LoadHotkey function i am getting the invalid cast exception.

Thanks.

Venu



 
 
Lepaca





PostPosted: Common Language Runtime, How to make custom marshalling of com to .Net? Top

I don't know what s.Get() return, but here there is an error:

If v = vbEmpty Or v = vbNull Then

You have to use VarType(v)...

I am not sure, that this is the main problem... Do you can give the line where the error occurs