×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

My 2 cents

本文发表在 rolia.net 枫下论坛#pragma once

class SharedMem
{
private:
HANDLE memMappedFileHandle_;
char* dataPointer_;
std::string name_;
int size_;

public:
SharedMem(const char* name, bool createOrAttach, int size)
{
size_ = size;
name_ = name;

// TODO: parameter checking
// Create unique named mutex
//guard = new Mutex(false, name + "Mutex");

// Create or attach to shared memory segment
if(createOrAttach)
{
memMappedFileHandle_ = CreateFileMappingA(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
size,
name);
}
else
{
memMappedFileHandle_ = OpenFileMappingA(FILE_MAP_ALL_ACCESS, true, name);
}

if( memMappedFileHandle_ == INVALID_HANDLE_VALUE)
{
DWORD errorCode = GetLastError();
if(errorCode == ERROR_INVALID_HANDLE)
{
//"Shared memory segment already in use"
}
else
{
//"Unable to access shared memory segment. GetLastError = " + errorCode;
}
}

dataPointer_ = reinterpret_cast<char*>(MapViewOfFile(memMappedFileHandle_, FILE_MAP_ALL_ACCESS, 0, 0, 0));

if(dataPointer_ == NULL)
{
DWORD errorCode = GetLastError();
CloseHandle(memMappedFileHandle_);
memMappedFileHandle_ = NULL;
//"Unable to map shared memory segment. GetLastError = " + i
}
}

~SharedMem()
{
// always clean up unmanaged resources
if(dataPointer_ != NULL)
{
UnmapViewOfFile(dataPointer_);
}

if(memMappedFileHandle_ != NULL)
{
CloseHandle(memMappedFileHandle_);
}
}

const char* Name()
{
return name_.c_str();
}

int Size()
{
return size_;
}

char* GetBuffer()
{
return dataPointer_;
}

void Lock()
{
//guard.WaitOne();
}

void Unlock()
{
//guard.ReleaseMutex();
}

void SaveToTemp()
{
std::string tempPath("c:\\temp\\");
std::string saveToPath = tempPath + name_;
std::ofstream tempFile(saveToPath.c_str(), std::ofstream::binary);
tempFile.write(dataPointer_, size_);
tempFile.close();
}
};更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 请问VC++中如何用自定义(WM_USER+XXX)的消息传递结构体的数据。挺急的,多谢了!
    我用WM_COPYDATA传没问题。用自定义消息传递不过去。而且LPARANM为什么不能穿递double 数据。
    • LPARAM or WPARAM is pointer (32bits integer), you can't pass double to it. You can pass it a pointer pointing to your structure.
    • 1、可以把指针(比如结构指针)强制转换成LPARAM传递,这样传递什么参数都是没有问题的,SendMessage( WM_USER+n , , , (LPARAM) lpStruct);2、看见你把LPARAM 写成LPARANM, 是否有拼写错误?
    • 另外lPARAM是指镇类型,在C++里不能直接强制转换,但是可以间接的亚:(LPARAM)(LONG)fDoubleVar;
      • double is 8-bytes, LPARAM is 4-bytes. Even converting float (4-bytes) to LPARAM is not a good practice
        • Yeah, you right,
    • Maybe LZ wants to pass data between windows in different processes. You only can use WM_COPYDATA or shared memory for interprocess data transferring.
      • I cant use WM_COPYDATA, as many processes communicate together,
        • You still can use WM_COPYDATA if you create dummy window only for the purpose to tranfer data, because window message is targeted to that window.
          1. create dummy window for every process
          2. send WM_COPYDATA to the window of the process when you want to transfer data to that process.
          3. You can use GetWindowThreadProcessId to map window handle to process id.
          4. You can set the dummy window's caption to the process id. It will be easier for you to locate the window of specific process. Using FindWindow.
      • how to use shared memoryto transfer struct data. thanx
    • 条条大道通罗马?如果你是我的学生,0分.
      • 进程间传递struct数据,用自定义消息。是高手就指点一下吧,先谢了
        • 问班主吧,我只是闲杂人员.
    • 各位DX,我是想在进程间(就是2个程序之间)传递数据,在1个进程的内存空间的指针到另1个进程是没意义的。多给点建议把。
      • .
        CreateFileMapping,MapViewOfFile,(write your data),UnmapViewOfFile,GetWindowThreadProcessId,OpenProcess,DuplicateHandle,SendMessage,CloseHandle(s). At another process,MapViewOfFile,(read your data),UnmapViewOfFile,CloseHandle.
      • NamePipe, or socket.
      • you can share data using DLL, memory-mapped files, com object, even sockets...
        • good answer
      • My 2 cents
        本文发表在 rolia.net 枫下论坛#pragma once

        class SharedMem
        {
        private:
        HANDLE memMappedFileHandle_;
        char* dataPointer_;
        std::string name_;
        int size_;

        public:
        SharedMem(const char* name, bool createOrAttach, int size)
        {
        size_ = size;
        name_ = name;

        // TODO: parameter checking
        // Create unique named mutex
        //guard = new Mutex(false, name + "Mutex");

        // Create or attach to shared memory segment
        if(createOrAttach)
        {
        memMappedFileHandle_ = CreateFileMappingA(
        INVALID_HANDLE_VALUE,
        NULL,
        PAGE_READWRITE,
        0,
        size,
        name);
        }
        else
        {
        memMappedFileHandle_ = OpenFileMappingA(FILE_MAP_ALL_ACCESS, true, name);
        }

        if( memMappedFileHandle_ == INVALID_HANDLE_VALUE)
        {
        DWORD errorCode = GetLastError();
        if(errorCode == ERROR_INVALID_HANDLE)
        {
        //"Shared memory segment already in use"
        }
        else
        {
        //"Unable to access shared memory segment. GetLastError = " + errorCode;
        }
        }

        dataPointer_ = reinterpret_cast<char*>(MapViewOfFile(memMappedFileHandle_, FILE_MAP_ALL_ACCESS, 0, 0, 0));

        if(dataPointer_ == NULL)
        {
        DWORD errorCode = GetLastError();
        CloseHandle(memMappedFileHandle_);
        memMappedFileHandle_ = NULL;
        //"Unable to map shared memory segment. GetLastError = " + i
        }
        }

        ~SharedMem()
        {
        // always clean up unmanaged resources
        if(dataPointer_ != NULL)
        {
        UnmapViewOfFile(dataPointer_);
        }

        if(memMappedFileHandle_ != NULL)
        {
        CloseHandle(memMappedFileHandle_);
        }
        }

        const char* Name()
        {
        return name_.c_str();
        }

        int Size()
        {
        return size_;
        }

        char* GetBuffer()
        {
        return dataPointer_;
        }

        void Lock()
        {
        //guard.WaitOne();
        }

        void Unlock()
        {
        //guard.ReleaseMutex();
        }

        void SaveToTemp()
        {
        std::string tempPath("c:\\temp\\");
        std::string saveToPath = tempPath + name_;
        std::ofstream tempFile(saveToPath.c_str(), std::ofstream::binary);
        tempFile.write(dataPointer_, size_);
        tempFile.close();
        }
        };更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • Let me tell you where to fish (instead of giving you fish): codeproject / codeguru or google
      • 原来您水平这么高,下次我可不能跟您说话了.
        本来可以学习 FRANKWOO说那句话的.
    • 我都几年没有干WINDOWS了,看了各位的表现,哎!这么简单而直接而经典的问题,居然全部回答都是莫名其妙!其中居然还有人号称很懂OS的.
      HINT; SHARE THE PAGE......................................................

      当年我给大学三年纪的非计算机专业讲课的时候,这只是一个小作业.
      • NB ahh NB
      • 原来自称UNKNOWN的倒是回答对了.
        • IUnknown
      • What the fuck does that have anything to do with OS? Unless you take OS class for windows...sigh...猴子称大王
        • 你是不是某些人的马甲,想让删贴?WINDOWS不是OS?在UNIX你又知道如何实现相同功能?看你也不懂.
          • Please tell me how to do it in Unix...
            There is no WM_USER in UNIX so what do you want to show off about? Besides, please tell me which university actually teaches the undergraduate students how to use windows message in os class.

            Thanks in advance for the eyes-opening answer!!!
            • 我猜对了,你也不懂UNIX.那请问您学的是什么OS?
              • 瞧你的小样儿
                楼主说的是WM_USER+X, share info between processes 是后来的事情了。我就问你how to do WM_USER+X share double type information in UNIX. 到底哪个学校出来的垃圾?操作系统课说windows? 如果你可以说服我你是技术大拿,我雇你没问题。已经在rolia上招了好几个好样儿的中国兄弟了,就是看不惯你。怎么的?
                • 宽怀豁达, 友善互助
    • Correct answer: use memmap file.
    • 我原来以为中国教育失败在重智轻德,看了这些回复,才知道无智无德者大有人在
      好的回复没有几个, 相互讥讽来的倒是很快, 可悲呀