Writing a DLL for MacroScheduler - Won't load

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Nerodon
Newbie
Posts: 2
Joined: Thu Mar 03, 2016 4:56 pm

Writing a DLL for MacroScheduler - Won't load

Post by Nerodon » Thu Mar 03, 2016 5:03 pm

Hello there, I've been trying to write some basic C++ DLLs to supplement my scripts but I can never get macroshed to actually load it.

Whenever I use libload, I get a result of 0. Which means it didn't like it for some reason.
The DLL works fine when implicitely load from a small C++ app I made to test the DLL.

Here's the source of my barebone DLL.

Code: Select all

// File: SampleDLL.h
//
#ifndef INDLL_H
#define INDLL_H

extern "C"
{
   #ifdef EXPORTING_DLL
      extern __declspec(dllexport) int HelloWorld() ;
   #else
      extern __declspec(dllimport) int HelloWorld() ;
   #endif
}
#endif

Code: Select all

// SampleDLL.cpp
#include <windows.h>

#define EXPORTING_DLL
#include "sampleDLL.h"

BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved )
{
   return TRUE;
}

extern "C"
{
    int HelloWorld(void)
    {
       return 1;
    }
}
And the script I use to try and load it:

Code: Select all

Let>lib="C:\Users\fbeaucha\Documents\testlib\Release\testlib.dll"
LibLoad>lib,hDll
LibFunc>hDll,HelloWorld,r
LibFree>hDll
I read that __stdcall should be used for macroscheduler but even by replacing __declspec(dllexport) with __stdcall makes no difference, the loadlib function simply returns 0.

Any help would be greatly apreciated.

Nerodon
Newbie
Posts: 2
Joined: Thu Mar 03, 2016 4:56 pm

Re: Writing a DLL for MacroScheduler - Won't load

Post by Nerodon » Thu Mar 03, 2016 10:36 pm

So I found out my mistake. I will post my solution to share with other potential DLL enthusiasts, since it's a really cool feature but somewhat hard to use as many things can go wrong and it is hard to diagnose.

When doing this, I included quotation marks and LibLoad doesn't like quotation marks.

Code: Select all

Let>lib="C:\Users\fbeaucha\Documents\testlib\Release\testlib.dll"
LibLoad>lib,hDll
Removing them solved my issue with LibLoad

Code: Select all

Let>lib=C:\Users\fbeaucha\Documents\testlib\Release\testlib.dll
LibLoad>lib,hDll
But after that i also ran into the issue with LibFunc as passing parameters without using __stdcall will cause violations and crash the macro.

So defining my export functions as such worked

Code: Select all

extern __stdcall int HelloWorld(int Value) ;
At that point the exported function names gathered a bit of decoration Ex: "HelloWorld@4" but this can be solved with a .def file.

I hope this helps others in the future maybe!

kevinkalexander67
Newbie
Posts: 8
Joined: Tue Jul 11, 2017 2:36 pm

Re: Writing a DLL for MacroScheduler - Won't load

Post by kevinkalexander67 » Tue Jul 11, 2017 2:52 pm

I'm trying to write a DLL for Macro Scheduler using C#. I'm using the NuGet package "Unmanaged Exports (DllExport for .Net)". I'm exporting two functions, HideWnd and ShowWnd.

Code: Select all

        [DllExport("HideWnd", CallingConvention.StdCall)]
        public static int HideWnd(string sTitle)
        {
            int ret = 0;
            var win = FindWindowsWithText(sTitle);
            foreach (int wnd in win)
            {
                hwndHiddenWnd = wnd;
                ret = ShowWindow(hwndHiddenWnd, 0); //// SW_HIDE
            }
            return ret;
        }

        /// <summary>
        /// Shows the window.
        /// </summary>
        /// <returns>Whether the ShowWindow call fails or not</returns>
        [DllExport("ShowWnd", CallingConvention.StdCall)]
        public static int ShowWnd()
        {
            int ret = 0;
            ret = ShowWindow(hwndHiddenWnd, 1); ////SW_SHOWNORMAL
            hwndHiddenWnd = 0;
            return ret;
        }
    }
I'm building the DLL with x64 as the platform. But the library fails to load when compiled with x64. But without x64, you can't see the Exported functions. If I build the DLL with "Any CPU", I can load the library but it failed to find any functions. I'm in a catch-22. No exported functions without x64 - no Libload with x64. Has anyone had this problem? How did you resolve it?

Code: Select all

Let>Lib=D:\ArgoDev\trunk\ArgoEmpiScriptingTools\bin\x64\Release\ArgoEmpiScriptingTools.dll
LibLoad>Lib,hLib
LibFunc>hLib,HideWnd,funcHide,WinDiff
LibFunc>hLib,ShowWnd,funcShow
LibFree>hLib

kevinkalexander67
Newbie
Posts: 8
Joined: Tue Jul 11, 2017 2:36 pm

Re: Writing a DLL for MacroScheduler - Won't load

Post by kevinkalexander67 » Tue Jul 11, 2017 3:39 pm

I found the solution to my problem. It was to compile the C# DLL as x86. I guess because Macro Scheduler is x86 it wants to load x86 DLL's(?). But once I compiled my C# DLL as x86, it loaded and successfully called the HideWnd and ShowWnd functions from the Macro Scheduler script.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts