?
Solved

VB.net trouble converting VBA code

Posted on 2014-04-09
3
Medium Priority
?
542 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:Murray Brown
[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 2000 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:Murray Brown
ID: 39990716
Thanks very much
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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