Solved

VB.net trouble converting VBA code

Posted on 2014-04-09
3
526 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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