国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Sharing a single dll among multiple processes.

Sharing a single dll among multiple processes.

  • Wednesday, November 22, 2006 5:21 PM
    Mansoor Ali
     

     

    Hello,

    I have a DLL, created using VC7 and that DLL is to be used by two EXEs, I need to know, whether it is possible load the DLL only once, so that the same instance is used by both the EXEs. If its possible then how it is done.

    Thanx.

     

     

Answers

  • Wednesday, November 22, 2006 6:39 PM
     

I hope you need data that will be shared cross two processes.

See, this thread.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=921874&SiteID=1

You can declare process shared section

#pragma section(".shared", read, write, shared)

 

Then use __declspec(allocate(".shared")) to declare shared data.

#define SHARED __declspec(allocate(".shared"))

SHARED volatile LONG g_nInstances = 0; //Cross process shared varible of long tipe

SHARED volatile MSG msg = {0}; //Cross process shared structure (MSG type)

You also, can use WM_COPYDATA message for cross process interaction.

Compile this Win32 console application. Run many instances of this application. See, the output. First you see a shared section.

// ProcessMarshal.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <windows.h>

#include <iostream>

#include <time.h>

using namespace std;

#pragma section(".shared", read, write, shared)

#define SHARED __declspec(allocate(".shared"))

SHARED volatile //volatile required because a compiler can generate a code, that will store and change data in register.

//thats why you can unpredictable results.

LONG g_nInstances = 0;

SHARED volatile MSG msg = {0};

void CopyData();

int _tmain(int argc, _TCHAR* argv[])

{

InterlockedIncrement(&g_nInstances);// Synchronization because another process can access for

// this field. Interlocked is enough.

cout<<g_nInstances<<endl;

HANDLE hMutex = ::CreateMutex(NULL, FALSE, _T("SyncShared")); //Name required to access the same mutex from different processes.

if(WAIT_FAILED != ::WaitForSingleObject(hMutex, INFINITE))

// Synchronization by mutex because you need update entire structure.

// You cannot use interlocked here, because some field can update one process, some a second.

{

msg.message++;

msg.time += 10;

cout<<"msg.message = "<<msg.message<<endl;

cout<<"msg.time = "<<msg.time<<endl;

ReleaseMutex(hMutex);

}

// Sends a structure vs WM_COPYDATA.

CopyData();

cin.get();

return 0;

}

bool isAny = false;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)

{

TCHAR name[256];

GetWindowText(hwnd, name, 255);

if(_tcscmp(name, _T("Form1")) == 0)

{

MSG msg = {NULL};

msg.message = g_nInstances;

msg.time = 31;

COPYDATASTRUCT cd = {0};

cd.cbData = sizeof(msg);

cd.dwData = 1;

cd.lpData = &msg;

cout<<"Send Copy Data"<<endl;

SendMessage(hwnd, WM_COPYDATA, NULL, (LPARAM)&cd);

isAny = true;

}

return 1;

}

void CopyData()

{

EnumWindows(&EnumWindowsProc, NULL);

if(!isAny)

cout<<"No windows"<<endl;

}

All Replies

  • Wednesday, November 22, 2006 6:39 PM
    Aleksandr Tokarev
     

    I hope you need data that will be shared cross two processes.

    See, this thread.

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=921874&SiteID=1

    You can declare process shared section

    #pragma section(".shared", read, write, shared)

     

    Then use __declspec(allocate(".shared")) to declare shared data.

    #define SHARED __declspec(allocate(".shared"))

    SHARED volatile LONG g_nInstances = 0; //Cross process shared varible of long tipe

    SHARED volatile MSG msg = {0}; //Cross process shared structure (MSG type)

    You also, can use WM_COPYDATA message for cross process interaction.

    Compile this Win32 console application. Run many instances of this application. See, the output. First you see a shared section.

    // ProcessMarshal.cpp : Defines the entry point for the console application.

    //

    #include "stdafx.h"

    #include <windows.h>

    #include <iostream>

    #include <time.h>

    using namespace std;

    #pragma section(".shared", read, write, shared)

    #define SHARED __declspec(allocate(".shared"))

    SHARED volatile //volatile required because a compiler can generate a code, that will store and change data in register.

    //thats why you can unpredictable results.

    LONG g_nInstances = 0;

    SHARED volatile MSG msg = {0};

    void CopyData();

    int _tmain(int argc, _TCHAR* argv[])

    {

    InterlockedIncrement(&g_nInstances);// Synchronization because another process can access for

    // this field. Interlocked is enough.

    cout<<g_nInstances<<endl;

    HANDLE hMutex = ::CreateMutex(NULL, FALSE, _T("SyncShared")); //Name required to access the same mutex from different processes.

    if(WAIT_FAILED != ::WaitForSingleObject(hMutex, INFINITE))

    // Synchronization by mutex because you need update entire structure.

    // You cannot use interlocked here, because some field can update one process, some a second.

    {

    msg.message++;

    msg.time += 10;

    cout<<"msg.message = "<<msg.message<<endl;

    cout<<"msg.time = "<<msg.time<<endl;

    ReleaseMutex(hMutex);

    }

    // Sends a structure vs WM_COPYDATA.

    CopyData();

    cin.get();

    return 0;

    }

    bool isAny = false;

    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)

    {

    TCHAR name[256];

    GetWindowText(hwnd, name, 255);

    if(_tcscmp(name, _T("Form1")) == 0)

    {

    MSG msg = {NULL};

    msg.message = g_nInstances;

    msg.time = 31;

    COPYDATASTRUCT cd = {0};

    cd.cbData = sizeof(msg);

    cd.dwData = 1;

    cd.lpData = &msg;

    cout<<"Send Copy Data"<<endl;

    SendMessage(hwnd, WM_COPYDATA, NULL, (LPARAM)&cd);

    isAny = true;

    }

    return 1;

    }

    void CopyData()

    {

    EnumWindows(&EnumWindowsProc, NULL);

    if(!isAny)

    cout<<"No windows"<<endl;

    }

  • Wednesday, November 22, 2006 6:44 PM
    Aleksandr Tokarev
     

    To load one instance of a dll in two process imposible. Each process loads its own dll-instance. But, share data cross process posible. See, above post.

    Memory pages with code somtimes shared, across process, but I think it nevermind for you.

  • Thursday, November 23, 2006 9:03 AM
    Mansoor Ali
     
    I have a Dll that makes a socket connection with the server, and there are two client applications both use that Dll, I want only single connection to the server using that DLL.

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
VC++ DLL調(diào)式方法?。ㄞD(zhuǎn))
智能指針shared
178 f0602
Phpredis 
Win32控制臺程序的定時器實現(xiàn)
【轉(zhuǎn)】C++ 智能指針詳解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服