Chinaunix首页 | 论坛 | 博客
  • 博客访问: 61255
  • 博文数量: 17
  • 博客积分: 1611
  • 博客等级: 上尉
  • 技术积分: 207
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-21 18:23
文章分类

全部博文(17)

文章存档

2013年(2)

2012年(4)

2011年(3)

2010年(3)

2009年(5)

我的朋友

分类: Windows平台

2013-04-14 23:58:16

一个控制台程序,一个DLL。

dll的头文件。
#include


#define OK 1
#define NG 0


extern "C" _declspec(dllexport)  int printSomething(int left,int right);


int inPrintSomething(int left,int right);
//VOID CALLBACK callbackProc();
//BOOL CreateSubThread();
//DWORD WINAPI MyThreadProc (LPVOID lpParam);
VOID CALLBACK OnTime(HWND, UINT, UINT_PTR, DWORD);


Dll的实现
// RpcDll.cpp : 定义 DLL 应用程序的导出函数。
//


#include "stdafx.h"
#include "RpcDll.h"
#include


DWORD threadId;
DWORD dwSubThreadFlag = TRUE;
UINT_PTR timerId;
DWORD flag = 0;
_declspec(dllexport)  int printSomething(int left,int right)
{
    //CreateSubThread();
    timerId = SetTimer(NULL,NULL,1000,OnTime);
    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        if(msg.message==WM_TIMER)
        {
            DispatchMessage(&msg);
        }
    }


    return TRUE;
}


int inPrintSomething(int left,int right)
{
    printf("Do.\n");
    return OK;
}
//BOOL CreateSubThread()
//{
//    HANDLE hThread = CreateThread(NULL, NULL,MyThreadProc,NULL,0,&threadId);
//    if (NULL == hThread)
//    {
//        return FALSE;
//    }
//    printf("Thread id is :%d\n",threadId);
//    return TRUE;
//}
//DWORD WINAPI MyThreadProc (LPVOID lpParam)
//{
//    while(dwSubThreadFlag)
//    {
//        printf("SubThread running.\n");
//        Sleep(500);
//    }
//    return TRUE;
//}
VOID CALLBACK OnTime(HWND, UINT, UINT_PTR, DWORD)
{
    inPrintSomething(0,0);
    flag++;
}


控制台程序的实现,没有另外的头文件
// UseDll.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include


typedef int (*DLL_Do)(int left,int right);


int _tmain(int argc, _TCHAR* argv[])
{
    HMODULE hRpcDll = LoadLibrary("RpcDll.dll");
    if (NULL == hRpcDll)
    {
        printf("Load error.\n");
        return -1;
    }
    DLL_Do f_doSomething = (DLL_Do)GetProcAddress(hRpcDll,"printSomething");
    if (NULL == f_doSomething)
    {
        printf("Get function address error.\n");
        return -2;
    }
    f_doSomething(0,0);
return 0;
}

这是一个使用多媒体计时的例子
// MediaTime.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include
#include
#pragma comment(lib, "winmm.lib")


using namespace std;


void CALLBACK TimeProc(
    UINT uID,
    UINT uMsg,
    DWORD dwUser,
    DWORD dw1,
    DWORD dw2);




int flag = 0;
int _tmain(int argc, _TCHAR* argv[])
{


//启动计时器
    MMRESULT nIDTimerEvent = timeSetEvent(
        1000,//延时1秒
        0,
        TimeProc,
        0,
        (UINT)TIME_PERIODIC);


    if( nIDTimerEvent == 0 )
        cout<< "启动计时器失败" << endl;
    Sleep(10000);
    printf("5S end.\n");
    while(flag < 5)
    {
        //cout<< "flag is :" << flag << endl;
    }
return 0;
}


//回调过程(时钟到来,回调函数被系统自动调用)
void CALLBACK TimeProc(
    UINT uID,
    UINT uMsg,
    DWORD dwUser,
    DWORD dw1,
    DWORD dw2)
{
    cout<< "时钟到来" << endl;
    flag ++;
   // cout << "flag is :" << flag << "   in timeProc" << endl;
}


阅读(6073) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

uewdl2013-04-16 11:01:31

赞一个