AutoHotkey.DLL, AutoHotkey.Interop - VS2015, VSTO Topic is solved

Ask for help, how to use AHK_H, etc.
burque505
Posts: 1396
Joined: 22 Jan 2017, 19:37

AutoHotkey.DLL, AutoHotkey.Interop - VS2015, VSTO

19 Feb 2017, 16:59

Hi, I'm at my wits' end here.
I'm running VS 2015 (32-bit, it seems) on my 64-bit Win7 machine.

I started a VB.NET VSTO Word Document project.
The code I'm using runs fine in a WinForm, with the obvious modifications for controls.

I've tried adding references to AutoHotkey.DLL (both 64-bit and 32-bit) and AutoHotkey.Interop (both 64-bit and 32-bit).
The project builds, but when I run it I get the message below.
One thing that throws me is that VS Studio, when I add the reference to AutoHotkey.DLL, calls it "Interop.AutoHotkey"
So I have one reference to "Interop.AutoHotkey" and another to "AutoHotkey.Interop".

When I modify the code so that it in theory shouldn't need AutoHotkey.Interop, but just AutoHotkey.DLL, it won't load it. And it calls it Interop.AutoHotkey.

Since I THINK AutoHotkey.Interop is a wrapper around the DLL, I don't think I need to call it. I've tried it both with the reference to AutoHotkey.DLL and without,
same result either way (below).

All help and insight welcome and appreciated.

Code: Select all

************** Exception Text **************
Microsoft.VisualStudio.Tools.Applications.Runtime.CannotCreateStartupObjectException: Could not create an instance of startup object VSTOahk2.ThisDocument in assembly VSTOahk2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: Unable to load DLL 'AutoHotkey.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at AutoHotkey.Interop.AutoHotkeyDll.ahktextdll(String Code, String Options, String Parameters)
   at AutoHotkey.Interop.AutoHotkeyEngine.<>c.<.cctor>b__19_0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at VSTOahk2.ThisDocument..ctor(Factory factory, IServiceProvider serviceProvider) in C:\Users\Winter\Desktop\VB.NET\VSTOahk2\ThisDocument.vb:line 4
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.CreateEntryPoint(String entryPointTypeName)
   --- End of inner exception stack trace ---
   at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.CreateEntryPoint(String entryPointTypeName)
   at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.LoadEntryPoints(IntPtr serviceProvider)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Office.Runtime
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.60724.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Office.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Office.Runtime.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Security
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Security/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Security.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.Hosting
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.60724.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Hosting/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Hosting.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.Runtime
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.60724.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Runtime.dll
----------------------------------------
System.Deployment
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Deployment/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Deployment.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.ServerDocument
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.60724.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.ServerDocument/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Xml.Linq
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll
----------------------------------------
Microsoft.Office.Tools
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.60724.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.dll
----------------------------------------
Microsoft.Office.Tools.Word.Implementation
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.60724.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Word.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Word.Implementation.dll
----------------------------------------
Microsoft.Office.Tools.Common.Implementation
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.60724.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.Implementation.dll
----------------------------------------
Microsoft.Office.Tools.Word
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.60724.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Word/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Word.dll
----------------------------------------
VSTOahk2
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Winter/Desktop/VB.NET/VSTOahk2/bin/Debug/VSTOahk2.DLL
----------------------------------------
Microsoft.Office.Tools.Word.v4.0.Utilities
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.30319.1
    CodeBase: file:///C:/Users/Winter/Desktop/VB.NET/VSTOahk2/bin/Debug/Microsoft.Office.Tools.Word.v4.0.Utilities.DLL
----------------------------------------
AutoHotkey.Interop
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Winter/Desktop/VB.NET/VSTOahk2/bin/Debug/AutoHotkey.Interop.DLL
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 14.6.1590.0 built by: NETFXREL2
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
Microsoft.Office.Interop.Word
    Assembly Version: 15.0.0.0
    Win32 Version: 15.0.4613.1000
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.Office.Interop.Word/15.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Word.dll
----------------------------------------
office
    Assembly Version: 15.0.0.0
    Win32 Version: 15.0.4859.1000
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/office/15.0.0.0__71e9bce111e9429c/office.dll
----------------------------------------

The code behind is below:

Code: Select all

Imports AutoHotkey.Interop
Imports AutoHotkey

Public Class ThisDocument
    Public ahk = AutoHotkeyEngine.Instance
    Public ahkscript = CreateObject("AutoHotkey.Script")
    'This gets a String from a System.Byte[] array, which Is what scripts are in .resx
    Public script As String = Encoding.UTF8.GetString(My.Resources.PUMtest)
    Private Sub ThisDocument_Startup() Handles Me.Startup
        'Try
        ahkscript.ahktextdll("" & script)
        'Catch ex As Exception
        '    MessageBox.Show("Unknown exception")
        'End Try
    End Sub

    Private Sub ThisDocument_Shutdown() Handles Me.Shutdown

    End Sub

End Class
burque505
Posts: 1396
Joined: 22 Jan 2017, 19:37

Re: AutoHotkey.DLL, AutoHotkey.Interop - VS2015, VSTO

19 Feb 2017, 18:19

Wow, that was quick, HotKeyIt!
It was actually in SysWoW64 at the moment, it's the Win32w version.
And I had regsvr32 AutoHotkey.dll on it in that location.

I just copied the 64-bit dll into system32, ran regsvr32 AutoHotkey.dll and it runs without a hitch.
(This was without the AutoHotkey.Interop being loaded or referenced. I'll try that too. (Edit: This worked fine after registering AutoHotkey.Dll - only reference is to AutoHotkey.Interop))
I have Word 2016 and Windows both 64-bit. VS is 32-bit.

Thanks! Most excellent diagnosis.

PS. Just as a follow-up, that enabled me to add AutoHotKey pop-up menus to a a Word ribbon add-in. I added a tab, and a button to display the PUM test suite.

VSTO tools must be installed.
Create new project -> Visual Basic -> VSTO Addins -> Word 2013 and Word 2016 VSTO add-in

Resource: PUMtest.AHK, which is all the scripts in the test suite in a single file (THANKS, DEO!)
Reference: Autohotkey.Interop
Project -> Add New Item -> Office/SharePoint -> Ribbon (Visual Designer )
Tab1: Expand ControlId, change ControlIdType to "Custom"; Change Label to "My Custom Tab"
Group 1: Add button btnPUMtest. Double-click to open code editor, code is in btnPUMtest_Click below.

ThisAddIn.vb: unchanged.

MyRibbon.vb:

Code: Select all

Imports Microsoft.Office.Tools.Ribbon
Imports AutoHotkey.Interop
Imports System.IO
Imports System.Windows.Forms 'Only need this for the Catch block below, MessageBox requires it. Runs fine with no try/catch also.

'In the line in the Try block 
'	ahkscript.ahktextdll("" & script)
'the empty quotes appear to be indispensable. 
'The public variable "script" gets passed in to ahktextdll
'(although I'm still scratching my head as to why it works this way. But it works.)

Public Class MyRibbon
    Public ahk = AutoHotkeyEngine.Instance
    Public ahkscript = CreateObject("AutoHotkey.Script")
    'This gets a String from a System.Byte[] array, which Is what scripts are in .resx
    Public script As String = Encoding.UTF8.GetString(My.Resources.PUMtest)

    Private Sub MyRibbon_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnPumTest_Click(sender As Object, e As RibbonControlEventArgs) Handles btnPumTest.Click
        Try
            ahkscript.ahktextdll("" & script)
        Catch ex As Exception
            MessageBox.Show("Unknown exception")
        End Try
    End Sub
End Class

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 4 guests