window API一天一練之共享記憶體

2021-12-29 21:51:33 字數 2483 閱讀 1311

今天學習win下共享記憶體的程序通訊,仿照qt的共享記憶體類,封裝了乙個簡單的類。雖然比不上qt的類那麼完善和強大,但是通過學習也達到了理解win共享記憶體的方式。

[html] 

class winsharedmemory  

;  [html]  

winsharedmemory::winsharedmemory( string szkey /*= ""*/ ):m_memkey(szkey)  

,m_islock(false),m_mutex(null),m_pmemory(null),m_size(0),m_filemapping(null)  

winsharedmemory::~winsharedmemory()  

bool winsharedmemory::createsize( int size )  

if (size < 1)  

m_size = size;  

m_filemapping = createfilemapping(invalid_handle_value,null,page_readwrite,0,m_size,m_memkey.c_str());  

if (m_filemapping == null)  

if (getlasterror() == error_already_exists)  

return true;  

}    

void * winsharedmemory::data()  

const void * winsharedmemory::constdata() const  

bool winsharedmemory::attach()  

}  return true;  

}    

bool winsharedmemory::lock()  

else  

}    

bool winsharedmemory::isattach()  

bool winsharedmemory::unlock()  

return true;  

}    

int winsharedmemory::size()  

void winsharedmemory::setkey( string szkey )  

string winsharedmemory::key()  

string winsharedmemory::geterrormessage()  

bool winsharedmemory::detach()  

m_size = 0;  

m_pmemory = null;  

cleanhandle();  

return true;  

}    

void winsharedmemory::cleanhandle()  

if (m_mutex)  

}   

qt的測速程式**:

[html] 

sharedmemory::sharedmemory(qwidget *parent, qt::wflags flags)  

: qdialog(parent, flags)  

sharedmemory::~sharedmemory()  

void sharedmemory::readdatafrommemoryslot()  

sharedmemory.createsize(size);  

sharedmemory.attach();  

sharedmemory.lock();  

char *pmemory = (char*)sharedmemory.data();  

char *ptext = new char[size];  

strcpy(ptext,pmemory);  

sharedmemory.unlock();  

sharedmemory.detach();  

m_ptextedit->settext(qstring::fromstdstring(ptext));  

}    

void sharedmemory::writedatatomemoryslot()  

sharedmemory.createsize(size);  

sharedmemory.attach();  

sharedmemory.lock();  

char *pmemory = (char*)sharedmemory.data();  

string text = m_ptextedit->toplaintext().tostdstring();  

strcpy(pmemory,text.c_str());  

sharedmemory.unlock();  

}    

void sharedmemory::resizeevent( qresizeevent *event )     

window API一天一練之操作檔案和目錄

今天花了好長時間了解檔案和目錄相關的api 寫了簡單的驗證程式,如有問題之處,希望各位大神指正。比較多久不一一枚舉api的宣告之類的資訊了。功能 輸出檔案屬性資訊 引數 檔案屬性 void printfileattributemessage dword dwfileattribute else if...

SQL一天一練(二)

select prod id,prod name from products where prod name like fish 這條sql語句是找出所有以fish開頭的prod name 那麼如果這樣寫 select prod id,prod name from products where pr...

MFC一天一練 靜態選單操作

直入主題 一 如何給選單增加標記?uint cmenu checkmenuitem uintnidcheckitem,uintncheck 具體使用參考msdn 二 如何設定預設的選單選項?bool cmenu setdefaultitem uintuitem,boolfbypos false 具體...