Windows, DLL calling experiment


h_ttp://youtu.be/U7sIEsIYbpE


#include <cstdio>
#include <windows.h>
//#include "led.h"

// references:
// http://puremonkey2010.blogspot.tw/2010/08/c-export-dll-export-dll.html
//http://rainice0616.pixnet.net/blog/post/37272330-%E3%80%90c%2B%2B-%E6%87%B6%E4%BA%BA%E7%94%A8%E6%B3%95%E3%80%91%E8%A3%BD%E4%BD%9C%E5%8F%8A%E4%BD%BF%E7%94%A8-dll

//------------------------------------
// led.dll, export funtion declaration. gcc complied, no header file gnerated.
//--------------------------------
extern "C"
{
__declspec(dllexport) void led_set(int a);
}
extern "C"
{
__declspec(dllexport) int led_get();
}
//--------------------------------

// define function prototype for dll to call
typedef int (*fp_led_get)();        //pointer of led_get, return one byte
typedef void (*fp_led_set)(int a);    //pointer of led_set, one argument, no return value

int main()
{
HINSTANCE dll_handler = LoadLibraryA("led.dll");    //LoadLibraryA for 32 bit dll
if(dll_handler == NULL)
{
printf("Load Dll Error\n");
system("pause");
return false;    //exit this program
}

//retrive handle of dll's function1
fp_led_get led_get = (fp_led_get)GetProcAddress(dll_handler, "led_get");
if(led_get == NULL)
{
printf("Load Function Error\n");
system("pause");
}

//retrive handle of dll's function1
fp_led_set fc_led_set = (fp_led_set)GetProcAddress(dll_handler, "led_set");
if(led_get == NULL)
{
printf("Load Function Error\n");
system("pause");
}

//    int IO_Value = 0;

fc_led_set(1);        // turn off LED
//    IO_Value = led_get();
//    printf("IO_Value = %d\n", IO_Value);
printf("IO_Value = %d\n", led_get());
system("pause");

fc_led_set(0);        //turn on LED
//    IO_Value = led_get();
//    printf("IO_Value = %d\n", IO_Value);
printf("IO_Value = %d\n", led_get());
system("pause");

// Unload DLL file
FreeLibrary(dll_handler);

return true;
}


http://stackoverflow.com/questions/4897685/how-do-i-register-a-dll-on-windows-7-64-bit

regsvr32 /s led.Dll

If the DLL is 32 bit:

Copy the DLL to C:\Windows\SysWoW64\
In elevated cmd: %windir%\SysWoW64\regsvr32.exe %windir%\SysWoW64\namedll.dll

if the DLL is 64 bit:

Copy the DLL to C:\Windows\System32\
In elevated cmd: %windir%\System32\regsvr32.exe %windir%\System32\namedll.dll

I know it seems the wrong way round, but that's the way it works. See:

http://support.microsoft.com/kb/249873
Quote: "Note On a 64-bit version of a Windows operating system, there are two versions of the Regsv32.exe file:
The 64-bit version is %systemroot%\System32\regsvr32.exe.
The 32-bit version is %systemroot%\SysWoW64\regsvr32.exe.

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s