Solved

VB.net trouble converting VBA code

Posted on 2014-04-09
3
489 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

18 Experts available now in Live!

Get 1:1 Help Now