CxUtils使用

CxUtils使用

 

本文转自:http://blog.sina.com.cn/s/blog_6e0693f70100sawf.html

 

CxUtils是一款包含了很多实用函数的跨平台C++函数库。这个库里面支持包装了多线程操作,网络通讯,游戏摇杆编程,串口通信,共享内存操作,定时器,键盘和鼠标捕获,以及基本的数学运算。这个函数库可以跨平台应用在windows,linux,或其它操作系统上。本文主要介绍了CxUtils的安装方法,及其使用CxUtils创建共享内用于多线程通讯的例子。

1). 在http://sourceforge.net/projects/active-ist/files/Cross Platform Utilities/2.100612/下载CxUtils版本,这里我们选择2.100612版本。这里我们把CxUtils存在C:\MANUS\CommonSpace路径下。

2). 下载后解压,在路径“C:\MANUS\CommonSpace\cxutils\2.0\build\msvc9”处可以找到Visual Studio解决方案文件,然后打开。这里我们使用的是Visual Studio 2010版本,而原解决方案是Visual Studio 2008生成的,所以需要转换。

Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用CxUtils <wbr>2.100612

3). 直接编译,链接,然后生成动态链接库dll和静态导出库lib文件。我们可以把生成的dll和lib都复制到一个公用文件夹中,再把这个公用文件夹的路径设置到系统环境变量PATH中,这样程序以后调用dll的时候就比较方便了。 环境设置方法如下Control Panel -> System and Security -> System -> Advanced system settings -> System Properties -> Advanced -> Environment Variables -> System variables -> Path

Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用CxUtils <wbr>2.100612
这里我们建立的公用文件夹的路径为C:\MANUS\CommonSpace\bin

Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用CxUtils <wbr>2.100612


4). 新建一个Visual Studio Win32 Console项目,命名为test CxUtils,test CxUtils.cpp代码后附。


5). 打开Project -> test CxUtils Property Pages -> Configuration Properties -> VC++ Directories -> Executable Directories,添加可执行文件目录 C:\MANUS\CommonSpace\bin

Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用CxUtils <wbr>2.100612

6). 打开Project -> test CxUtils Property Pages -> Configuration Properties -> VC++ Directories -> Include Directories,添加头文件目录 C:\MANUS\CommonSpace\cxutils\2.0\include

Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用CxUtils <wbr>2.100612

7). 打开Project -> test CxUtils Property Pages -> Configuration Properties -> VC++ Directories -> Library Directories,添加库文件目录 C:\MANUS\CommonSpace\cxutils\2.0\lib

Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用CxUtils <wbr>2.100612


8). 打开Project -> test CxUtils Property Pages -> Linker -> Input -> Additional Dependencies,添加导出库文件 libcxutils_32_d.lib 这里我们使用的是debug版本。

Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用CxUtils <wbr>2.100612

9). 编译,连接,生成exe文件。为了调试方便,我们把C:\MANUS\CommonSpace\bin添加到debug的环境变量中去。添加内容:PATH=%PATH%;C:\MANUS\CommonSpace\bin

Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用CxUtils <wbr>2.100612

test CxUtils.cpp代码

#include "stdafx.h"

#include <iostream>

#include <vector>

#include "cxutils/ipc/mappedmemory.h"

#include "cxutils/thread.h"

#include "cxutils/keyboard.h"

using namespace std;

using namespace CxUtils;

// Number of threads writing to shared memory.

const int NUM_THREADS = 10;

void WriteToMemory(void *arg);

class ThreadData

{

public:

ThreadData()

{

mPos = 0;

}

~ThreadData() {}

string mName;

unsigned int mPos;

static bool mLoop;

};

bool ThreadData::mLoop = true;

int main(int argc, char **argv)

{

char name[128] = "Mem_Example";

Thread threads[NUM_THREADS];

MappedMemory mem;

ThreadData data[NUM_THREADS];

cout << "Creating Shared Memory...";

if(!mem.CreateMappedMemory(name, NUM_THREADS*sizeof(unsigned short)))

{

cout << "Could not create shared/mapped memory: " << name << endl;

return 0;

}

cout << "Success\n";

// If no additional program arguments passed, create threads

// that will write to shared memory. You can also use the

// cxipc_client.cpp program to write to shared memory also.

if(argc == 1)

{

for(int i = 0; i < NUM_THREADS; i++)

{

// Set encode position for the thread to use.

data[i].mPos = i*sizeof(unsigned short);

data[i].mName = name;

// Create thread.

threads[i].CreateThread(WriteToMemory, (void *)(&data[i]));

}

}

while(true)

{

if(GetChar() == 27)

{

for(int i = 0; i < NUM_THREADS; i++)

{

data[i].mLoop = false;

}

break;

}

unsigned short count = 0;

mem->SetReadPos(0);

mem.Lock(); // Must lock for read/write safety.

for(int i = 0; i < NUM_THREADS; i++)

{

count = 0;

mem->Read(count);

cout << count << " ";

}

mem.Unlock(); // Must unlock to prevent deadlock.

cout << "\n";

SleepMs(1);

}

for(int i = 0; i < NUM_THREADS; i++)

{

data[i].mLoop = false;

SleepMs(1);

threads[i].StopThread();

}

return 0;

}

void WriteToMemory(void *arg)

{

string name;

unsigned int epos = 0;

unsigned short count = 0;

MappedMemory mem;

ThreadData *info = (ThreadData *)(arg);

if(!arg)

return;

name = info->mName; // Name of memory.

epos = info->mPos; // Write position.

SleepMs(50);

if(mem.OpenMappedMemory(name.c_str()) == 0)

{

cout << "Could not open shared/mapped memory: " << name << endl;

return;

}

while(info->mLoop)

{

mem.Lock();

mem->Write(++count, epos);

mem.Unlock();

SleepMs(1);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值