重庆金海棠养生会所:Sharing a single dll among multiple processes.

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 03:36:57

Sharing a single dll among multiple processes.

  • Wednesday, November 22, 2006 5:21 PMMansoor Ali   0 Sign In to Vote

     

    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  
0 Sign In to Vote

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

#include

#include

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<

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 = "<

cout<<"msg.time = "<

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"<

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

isAny = true;

}

return 1;

}

void CopyData()

{

EnumWindows(&EnumWindowsProc, NULL);

if(!isAny)

cout<<"No windows"<

}

All Replies

  • Wednesday, November 22, 2006 6:39 PMAleksandr Tokarev   0 Sign In to Vote

    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

    #include

    #include

    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<

    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 = "<

    cout<<"msg.time = "<

    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"<

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

    isAny = true;

    }

    return 1;

    }

    void CopyData()

    {

    EnumWindows(&EnumWindowsProc, NULL);

    if(!isAny)

    cout<<"No windows"<

    }

  • Wednesday, November 22, 2006 6:44 PMAleksandr Tokarev   0 Sign In to Vote

    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 AMMansoor Ali   0 Sign In to Vote 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.