サンプル(Ver9.35対応版)
目次−
DLL側から秀丸エディタの関数呼び出し−
サンプル(.net framework)
●.net framework側の例
//クラスライブラリでプロジェクト設定 「アセンブリをCOM参照可能にする」「COM相互運用の機能の登録」が必要。
//管理者で「regasm ClassLibrary1.dll /tlb:ClassLibrary1.tlb」が必要。32bitと64bitで違うので注意。
//または、#obj=createobject(@"C:\Folder\ClassLibrary1.dll","ClassLibrary1.Test1");としてユーザーごとの登録でも可能。
using System;
using System.Net;
using System.Runtime.InteropServices;
namespace ClassLibrary1
{
[Guid("0F3B0368-61E4-4E2D-BB3C-86ADC8DFD602")] //guidgenで生成する適当な別の値にしてください
public class Test1
{
[DllImport("user32.dll")]
public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string lpFileName);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
static extern IntPtr GlobalFree(IntPtr hMem);
public delegate IntPtr HIDEMARU_GETCURRENTWINDOWHANDLE();
public delegate Boolean HIDEMARU_GETCURSORPOSUNICODE( out int lineno, out int column );
public delegate IntPtr HIDEMARU_GETSELECTEDTEXTUNICODE();
public IntPtr TestMethod()
{
IntPtr hmod = GetModuleHandle(null); //hidemaru.exe自身
IntPtr pfnHidemaru_GetCurrentWindowHandle = GetProcAddress(hmod, "Hidemaru_GetCurrentWindowHandle");
HIDEMARU_GETCURRENTWINDOWHANDLE Hidemaru_GetCurrentWindowHandle = (HIDEMARU_GETCURRENTWINDOWHANDLE)Marshal.GetDelegateForFunctionPointer(pfnHidemaru_GetCurrentWindowHandle, typeof(HIDEMARU_GETCURRENTWINDOWHANDLE));
IntPtr pfnHidemaru_GetCursorPosUnicode = GetProcAddress(hmod, "Hidemaru_GetCursorPosUnicode");
HIDEMARU_GETCURSORPOSUNICODE Hidemaru_GetCursorPosUnicode = (HIDEMARU_GETCURSORPOSUNICODE)Marshal.GetDelegateForFunctionPointer(pfnHidemaru_GetCursorPosUnicode, typeof(HIDEMARU_GETCURSORPOSUNICODE));
IntPtr pfnHidemaru_GetSelectedTextUnicode = GetProcAddress(hmod, "Hidemaru_GetSelectedTextUnicode");
HIDEMARU_GETSELECTEDTEXTUNICODE Hidemaru_GetSelectedTextUnicode = (HIDEMARU_GETSELECTEDTEXTUNICODE)Marshal.GetDelegateForFunctionPointer(pfnHidemaru_GetSelectedTextUnicode, typeof(HIDEMARU_GETSELECTEDTEXTUNICODE));
//Hidemaru_GetCurrentWindowHandleのテスト
IntPtr hwndHidemaru = IntPtr.Zero;
if( Hidemaru_GetCurrentWindowHandle != null ) {
hwndHidemaru = Hidemaru_GetCurrentWindowHandle();
}
if( hwndHidemaru != IntPtr.Zero) {
MessageBox(hwndHidemaru, "Hello Hidemaru.", "MessageBox on Hidemaru", 0);
}
//Hidemaru_GetCursorPosUnicodeのテスト
int lineno = 0;
int column = 0;
Boolean f = Hidemaru_GetCursorPosUnicode( out lineno, out column );
MessageBox(hwndHidemaru, lineno + "," + column, "lineno, column", 0);
//Hidemaru_GetSelectedTextUnicodeのテスト
string strText = null;
IntPtr hGlobal = Hidemaru_GetSelectedTextUnicode();
if (hGlobal != IntPtr.Zero) {
var pwsz = GlobalLock( hGlobal );
if( pwsz != IntPtr.Zero ) {
strText = Marshal.PtrToStringUni(pwsz);
MessageBox(hwndHidemaru, strText, "Selected Text", 0);
GlobalUnlock( hGlobal );
}
GlobalFree( hGlobal );
}
return hwndHidemaru; //戻り値のテスト
}
}
}
●マクロ側の例
#obj=createobject("ClassLibrary1.Test1");
//または、#obj=createobject(@"C:\Folder\ClassLibrary1.dll","ClassLibrary1.Test1");としてユーザーごとの登録でも可能。
if(getresultex(10)!=0) {
message "マクロのhidemaruhandle:\n"
+ str(hidemaruhandle(0))+"\n\n"
+".net frameworkのDLLで呼んだHidemaru_GetCurrentWindowHandle:\n"
+ str(member(#obj,"TestMethod"));
} else {
message "createobject失敗";
}
endmacro;