Solved

VB.net trouble converting VBA code

Posted on 2014-04-09
3
495 Views
Last Modified: 2014-04-09
Hi. I am trying to convert the VBA code below into VB.net code and still cannot work out the following errors:
Error      1      'As Any' is not supported in 'Declare' statements.
Error      2      'StrPtr' is not declared. It may be inaccessible due to its protection level.

I tried lParam As IntPtr but got the error
Error      2      Value of type 'Invoicing.Autohotkey.COPYDATASTRUCT' cannot be converted to 'System.IntPtr'.      



http://www.autohotkey.com/board/topic/102577-excel-sendmessage-wm-copydata-to-ahk-script/

Option Explicit

Type COPYDATASTRUCT
    dwData As Long
    cbData As Long
    lpData As Long
End Type

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long

Const WM_COPYDATA = &H4A
Const LNULL = 0&

Public Function GetAhkHwnd()
    Dim hwnd As Long
    Dim iCounter As Integer
    hwnd = FindWindow("AutoHotkey", "D:\Autohotkey\test\Receiver.ahk - AutoHotkey v1.1.07.03")
    While hwnd = 0 And iCounter < 5
        Shell ("C:\Program Files\AutoHotkey\AutoHotkey.exe D:\Autohotkey\test\Receiver.ahk")
        hwnd = FindWindow("AutoHotkey", "D:\Autohotkey\test\Receiver.ahk - AutoHotkey v1.1.07.03")
        iCounter = iCounter + 1
    Wend
    GetAhkHwnd = hwnd
End Function

Public Function test(sMsg As String)
    Dim lReturn As Long
    Dim hwnd As Long
    Dim cds As COPYDATASTRUCT
    'Dim sMsg As String
    Dim iErrReturn As Integer
        
    hwnd = GetAhkHwnd()
    If hwnd = 0 Then
        iErrReturn = MsgBox("Could not start AHK!", vbOK + vbCritical + vbApplicationModal, "Test SendMessage")
    Else
        cds.dwData = 0
        cds.cbData = Len(sMsg) * 2 + 2
        cds.lpData = StrPtr(sMsg)
        lReturn = SendMessage(hwnd, WM_COPYDATA, LNULL, cds)
        If lReturn <> 1 Then MsgBox ("Message not received by Receiver.ahk!")
    End If
End Function

Open in new window

0
Comment
Question by:murbro
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39990071
For error 1, change

As Any

to

As Object


Which line throws error 2?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39990204
There is more than that.

What was an Integer in VB6 is a Short in .NET. You need to replace all the Integer by Short.

What was a Long in VB6 is an Integer in .NET. You need to replace all the Long by Integer.

Pointers are not directly supported in .NET, so StrPtr has no direct equivalent. You need to use one of the 4 Marshal.PtrToString... methods provided by .NET in order to simulate it. You may have to fool around before you find the one that works for your needs.

Trying to convert code directly from VB6 to VB.NET is usually a bad idea, too many things have changed.

You are usually better to find the .NET way of doing the same thing.

Very often, something that required 60 lines of codes in VB6 can be done in 1 line in .NET, knowing the class and method that can do it.

An trying to make old software work with .NET is not always possible. If you try to put 1950's gasoline in a modern car, you will damage it because of the lead. You are usually better to find a new version of the software that is compatible with .NET (maybe http://www.autohotkey.com/board/topic/23884-net-framework-interop/) or more modern software that can do the same thing.
0
 

Author Closing Comment

by:murbro
ID: 39990716
Thanks very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now