1. 程式人生 > >調用動態庫!

調用動態庫!

include 客戶端 動態 資源

#include<windows.h>

#include<iostream>

//客戶端初始化 獲取handle上下

typedef int(*CltSocketInit)(void **handle);

//客戶端發報文

typedef int(*CltSocketSend)(void *handle, unsigned char *buf, int buflen);

//客戶端收報文

typedef int(*CltSocketRev)(void *handle, unsigned char *buf, int *buflen);

//客戶端釋放資源

typedef int(*CltSocketDestory)(void *handle);//以上都是指針函數

int main(){


HINSTANCE hinstance = NULL;

hinstance = ::LoadLibrary("D:\\SocketClient.dll");//獲得句柄

CltSocketInit cltSocketInit = (CltSocketInit)GetProcAddress(hinstance, "cltSocketInit");//獲取函數地址傳給函數指針!

CltSocketSend cltSocketSend = (CltSocketSend)GetProcAddress(hinstance, "cltSocketSend");

CltSocketRev cltSocketRev = (CltSocketRev)::GetProcAddress(hinstance, "cltSocketRev");

CltSocketDestory cltSocketDestory = (CltSocketDestory)::GetProcAddress(hinstance, "cltSocketDestory");

void *handle = NULL;

unsigned char buf[100];

int buflen = 10;

memcpy(buf, "ddddddddddssssssssss", 10);


unsigned char out[100] = { 0 };

int outlen = 0;


int ret = cltSocketInit(&handle);


ret = cltSocketSend(handle, buf, buflen);

ret = cltSocketRev(handle, out, &outlen);

ret = cltSocketDestory(handle);


printf("out:%s", out);

system("pause");


}


調用動態庫!