⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample18_dll_import.c

📁 机械工业出版社 Lab Windows/CVI逐步深入与开发实例源代码
💻 C
字号:
#include <windows.h>
#include <sample18_dll.h>

/* The two macros below are used as error return codes */
/* in case the DLL does not load, or is missing one or */
/* more functions, respectively.  You must define them */
/* to whatever values are meaningful for your DLL.     */
#define kFailedToLoadDLLError     -1
#define kCouldNotFindFunction     -1

static HINSTANCE DLLHandle;

/* Declare the variables that hold the addresses of the function   */
/* pointers.                                                       */
static void (__stdcall *DrawTextInWindow_Ptr)(int theWindow, short x,
                                              short y, char *text,
                                              char *fontName,
                                              short pointSize, short bold,
                                              short italic, short shadow,
                                              long color);
static void (__stdcall *GetVKLock_Ptr)(short *capLock, short *numLock,
                                       short *scrollLock);
static void (__stdcall *ReadRegString_Ptr)(HKEY hRootKey, char *psubKey,
                                           int index, char *tagName,
                                           int *tagNameLen, char *tagValue,
                                           int *tagValueLen);
static void (__stdcall *WriteRegString_Ptr)(HKEY hRootKey, char *psubKey,
                                            int index, char *tagName,
                                            char *tagValue);


/* Load the DLL and get the addresses of the functions */
static int LoadDLLIfNeeded(void)
{
    if (DLLHandle)
        return 0;

    DLLHandle = LoadLibrary("sample18_dll.dll");
    if (DLLHandle == NULL) {
        return kFailedToLoadDLLError;
        }

    if (!(DrawTextInWindow_Ptr = (void*) GetProcAddress(DLLHandle, 
         "DrawTextInWindow")))
        goto FunctionNotFoundError;

    if (!(GetVKLock_Ptr = (void*) GetProcAddress(DLLHandle, "GetVKLock")))
        goto FunctionNotFoundError;

    if (!(ReadRegString_Ptr = (void*) GetProcAddress(DLLHandle, "ReadRegString")))
        goto FunctionNotFoundError;

    if (!(WriteRegString_Ptr = (void*) GetProcAddress(DLLHandle, 
         "WriteRegString")))
        goto FunctionNotFoundError;

    return 0;

FunctionNotFoundError:
    FreeLibrary(DLLHandle);
    DLLHandle = 0;
    return kCouldNotFindFunction;
}


/* Glue Code for each of the DLL functions */



void __stdcall DrawTextInWindow(int theWindow, short x, short y, char *text, 
                                char *fontName, short pointSize, short bold, 
                                short italic, short shadow, long color)
{
    int dllLoadError;

    if (dllLoadError = LoadDLLIfNeeded())
        return;
    (*DrawTextInWindow_Ptr)(theWindow, x, y, text, fontName, pointSize, 
                            bold, italic, shadow, color);
}


void __stdcall GetVKLock(short *capLock, short *numLock, short *scrollLock)
{
    int dllLoadError;

    if (dllLoadError = LoadDLLIfNeeded())
        return;
    (*GetVKLock_Ptr)(capLock, numLock, scrollLock);
}


void __stdcall ReadRegString(HKEY hRootKey, char *psubKey, int index, 
                             char *tagName, int *tagNameLen, char *tagValue, 
                             int *tagValueLen)
{
    int dllLoadError;

    if (dllLoadError = LoadDLLIfNeeded())
        return;
    (*ReadRegString_Ptr)(hRootKey, psubKey, index, tagName, tagNameLen, 
                         tagValue, tagValueLen);
}


void __stdcall WriteRegString(HKEY hRootKey, char *psubKey, int index, 
                              char *tagName, char *tagValue)
{
    int dllLoadError;

    if (dllLoadError = LoadDLLIfNeeded())
        return;
    (*WriteRegString_Ptr)(hRootKey, psubKey, index, tagName, tagValue);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -