Dev-c++ 동적 라이브러리 제작의 간단한 사용
9406 단어 windows
1、새 dll 프로젝트
2、생성된 dllmain과 dll.지우다
3. 자신의 헤더 파일과 원본 파일 추가
4. 헤더 파일에 DLLIMPORT 매크로 추가
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
5. 헤더 파일의 성명 함수에 내보낼 함수에 DLLIMPORT 매크로와 extern "C"를 추가합니다
extern "C" DLLIMPORT int CreateGetPictureMainThread();
extern "C" DLLIMPORT int GetPictureData(char *OutData, int *valid_size, int no);
6. 내보내야 할 함수체에 DLLIMPORT 매크로를 원본 파일에 추가
#ifndef _GET_PICTURE_H_
#define _GET_PICTURE_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8187
#define BUFFER_SIZE 1024
#define MAX_PICTURE_SIZE (400*1024)
#define MAX_CACHE_PICTURE 5
#define MAX_FILE_NUM 50
#define COMMAND_SIZE 10
typedef struct PICTURE_INFO
{
unsigned int picture_valid_size;
char picture_data[MAX_PICTURE_SIZE];
}PictureInfo;
extern PictureInfo picture_info[MAX_CACHE_PICTURE];
extern char cmd[COMMAND_SIZE];
int Connect_server(char *ip, SOCKET *sockClient);
int SendRequest(int sockfd);
int SendFinishAck(int sockfd);
int GetTimeArg(int sockfd);
int RecvPicture(PictureInfo *pInfo, int sockfd);
int WritePictureToFile(PictureInfo *pInfo, char *file_name);
int GetIPFromConfigFile(char *ip);
int SetFileName(char *file_name);
void *GetPictureMainThread(void *arg);
extern "C" DLLIMPORT int CreateGetPictureMainThread();
extern "C" DLLIMPORT int GetPictureData(char *OutData, int *valid_size, int no);
#endif
#include "GetPictureThread.h"
PictureInfo picture_info[MAX_CACHE_PICTURE];
char cmd[COMMAND_SIZE];
void *GetPictureMainThread(void *arg)
{
SOCKET sockClient;
char ip[16];
char file_name[20];
int nbytes;
static int index = 0;
memset(ip, 0, 16);
memset(file_name, 0, sizeof(file_name));
GetIPFromConfigFile(ip);
Connect_server(ip, &sockClient);
while(1)
{
nbytes = SendRequest(sockClient);
if (nbytes == 0 || nbytes == -1)
{
printf("The server has been closed.Try to reconnect the server...
");
closesocket(sockClient);
WSACleanup();
Connect_server(ip, &sockClient);
Sleep(1000);
}else
{
memset(cmd, 0, COMMAND_SIZE);
recv(sockClient, cmd, COMMAND_SIZE, 0);
if (strcmp(cmd, "NOT_READY") == 0)
{
Sleep(10);
continue;
}else if (strcmp(cmd, "READY") == 0)
{
printf("cmd: %s, index: %d
", cmd, index);
RecvPicture(&picture_info[index], sockClient);
SendFinishAck(sockClient);
GetTimeArg(sockClient);
//memcpy(file_name, "picture.jpg", sizeof(file_name));
SetFileName(file_name);
WritePictureToFile(&picture_info[index], file_name);
index++;
if (index == MAX_CACHE_PICTURE)
{
index = 0;
}
}
}
}
closesocket(sockClient);
WSACleanup();
}
DLLIMPORT int CreateGetPictureMainThread()
{
pthread_t pid;
if (pthread_create(&pid, NULL, GetPictureMainThread, NULL) == -1)
{
printf("pthread_create failed.
");
exit(1);
}
return 0;
}
int SetFileName(char *file_name)
{
static int count = 1;
char suffix[] = ".jpg";
memset(file_name, 0, sizeof(file_name));
sprintf(file_name, "%d%s", count, suffix);
count++;
if (count > MAX_FILE_NUM)
{
count = 1;
}
return 0;
}
int Connect_server(char *ip, SOCKET *sockClient)
{
int err;
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return -1;
}
*sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr(ip);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(SERVER_PORT);
err = connect(*sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (err == 0)
{
printf("Connected successfully.
Wait to receive picture...
");
}else
{
printf("Connect failed.
");
}
return 0;
}
int SendRequest(int sockfd)
{
memset(cmd, 0, COMMAND_SIZE);
memcpy(cmd, "REQUEST", COMMAND_SIZE);
int length = send(sockfd, cmd, COMMAND_SIZE, 0);
if (length == -1)
{
printf("send failed.
");
}
return length;
}
int SendFinishAck(int sockfd)
{
memset(cmd, 0, COMMAND_SIZE);
memcpy(cmd, "RECV_OK", COMMAND_SIZE);
int length = send(sockfd, cmd, COMMAND_SIZE, 0);
if (length == -1)
{
printf("send failed.
");
}
return length;
}
int GetTimeArg(int sockfd)
{
long int time;
recv(sockfd, (char*)&time, 4, 0);
printf("Internet transfer time: %ld ms
", time);
recv(sockfd, (char*)&time, 4, 0);
printf("Capture picture whole time: %ld ms
", time);
return 0;
}
int RecvPicture(PictureInfo *pInfo, int sockfd)
{
unsigned int recvsize = 0;
unsigned int length = 0;
char buffer[BUFFER_SIZE];
char *Addr = pInfo->picture_data;
memset(pInfo, 0, sizeof(PictureInfo));
memset(buffer, 0, BUFFER_SIZE);
recv(sockfd, (char*)&pInfo->picture_valid_size, 4, 0);
printf("size: %d
", pInfo->picture_valid_size);
while(recvsize != pInfo->picture_valid_size)
{
length = recv(sockfd, buffer, BUFFER_SIZE, 0);
if (length < 0)
{
printf("Recieve Data From Server Failed!
");
break;
}
memcpy(Addr, buffer, length);
recvsize += length;
Addr += length;
memset(buffer, 0, BUFFER_SIZE);
}
return 0;
}
int WritePictureToFile(PictureInfo *pInfo, char *file_name)
{
unsigned int write_length = 0;
unsigned int length;
char *Addr = pInfo->picture_data;
FILE *pFile = fopen(file_name, "wb");
length = fwrite(Addr, sizeof(char), pInfo->picture_valid_size, pFile);
if (length == pInfo->picture_valid_size)
{
printf("Write File[%s] Finished[%dbytes]!
", file_name, length);
}else
{
printf("Write failed.
");
}
fclose(pFile);
return 0;
}
int GetIPFromConfigFile(char *ip)
{
char str[8];
char temp[24];
int n = 0;
FILE *pconf = fopen("config.txt", "r+");
if (pconf == NULL)
{
printf("Open file[config.txt] failed.
");
exit(1);
}
memset(temp, 0, 24);
n = fread(temp, sizeof(temp), 1, pconf);
memset(str, 0, 8);
memcpy(str, temp, 8);
memcpy(ip, temp+8, 16);
printf("%s
", temp);
fclose(pconf);
return 0;
}
DLLIMPORT int GetPictureData(char *OutData, int *valid_size, int no)
{
if (OutData == NULL)
{
*valid_size = picture_info[no].picture_valid_size;
}else
{
*valid_size = picture_info[no].picture_valid_size;
//OutData = picture_info[no].picture_data;
memset(OutData, 0, sizeof(OutData));
memcpy(OutData, picture_info[no].picture_data, picture_info[no].picture_valid_size);
}
return 0;
}
2. dll의 호출 방법
1. 새 콘솔 프로그램 만들기
#include
#include
#include
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int size;
typedef int (*FUNT)(void);
typedef int (*GETPICTURE)(char *, int *, int);
HINSTANCE Hint = LoadLibrary("GetPictureThread.dll");//load dll path
FUNT CreateGetPictureMainThread = (FUNT)GetProcAddress(Hint, "CreateGetPictureMainThread");//get function handle
GETPICTURE GetPictureData = (GETPICTURE)GetProcAddress(Hint, "GetPictureData");
if (CreateGetPictureMainThread == NULL)
{
printf("load dll failed.
");
exit(1);
}
CreateGetPictureMainThread();
getchar();
GetPictureData(0, &size, 0);
char pData[size];
GetPictureData(pData, &size, 0);
printf("size: %d
", size);
FILE *pFile = fopen("mypicture.jpg", "wb");
if (pFile == NULL)
{
printf("open failed.
");
exit(1);
}
fwrite(pData, 1, size, pFile);
fclose(pFile);
getchar();
FreeLibrary(Hint);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
제한된 크기의 디렉토리를 만드는 방법오늘 저는 장치에 공간이 없을 때 백업 중에 응용 프로그램이 어떻게 작동하는지 테스트(및 수정)하는 작업이 있습니다. 결과적으로 "남은 공간 없음"오류로 백업이 실패하면 새 파일이 없어야 합니다. 지금까지 문제를 재...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.