Solved

VB.net trouble converting VBA code

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

772 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