Link to home
Start Free TrialLog in
Avatar of Brad Brett
Brad BrettFlag for United States of America

asked on

Two applications cummunication through DLL

I have the following two applications:
App1.exe
App2.exe

One dll:
MyAPI.dll

Dll methods:
SetVar();
GetVar();

What I am trying to do is to make the both applications communicate through the DLL file, for example: If App1.exe used the dll method SetVar, I want to be able to get that var using GetVar() in App2.exe
Avatar of ramrom
ramrom
Flag of United States of America image

What have you tried? Where are you stuck?
Avatar of Brad Brett

ASKER

I am trying to use DLL to make communication between two applications that use the same DLL file, I haven't tried anything yet.
That's not really an issue, the only problem is that you will have to declare a shared data segment for that to work - and don't expect to be able to store objects there, this will only work for POD types. See http://msdn.microsoft.com/en-us/library/h90dkhs0%28VS.80%29.aspx ("How do I share data in my DLL with an application or with other DLLs?") - the gist is to

#pragma data_seg (".myseg")
   int SampleInt = 0; 
   char SampleText[32] = "hello world";
#pragma data_seg()
#pragma comment(linker,"/SECTION:.MYSEC,RWS.")

Open in new window


Then you can impement e.g.

int GetSampleInt() { return SampleInt;}
void SetSampleInt(int n) { SampleInt = n;}

Open in new window


etc.
I have tried using the following code:
#include "windows.h"
#pragma data_seg(".myseg")
int m_Var;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.MYSEC,RWS.")

extern "C" __declspec(dllexport) void SetVar(int var);
extern "C" __declspec(dllexport) int GetVar();

BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID lpvReserved)
{
	return TRUE;
}

extern "C" __declspec(dllexport) void SetVar(int var)
{
	m_Var = var;
}

extern "C" __declspec(dllexport) int GetVar()
{
	return m_Var;
}

Open in new window


But, still not working.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Still having the same problem, BTW, I am using C# to deal with the DLL:

App1.exe:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("dllfile.dll")]
        public static extern int GetVar();

        [DllImport("dllfile.dll")]
        public static extern void SetVar(int var);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetVar(50);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetVar().ToString());
        }
    }
}

Open in new window

Is there a reason for choosing DLL?

What is your "higher-level objective"?
>>Still having the same problem

Could you be a bit more specific about what the problem is? Trouble setting the value, trouble obtaining it, a crash or a build issue?
The problem is that the variable is not shared between the two applications, App1.exe could have the variable set to 10 while the other application could have the same variable set to 20.
Ooops, I spotted a copy&paste accident  the line

#pragma comment(linker, "/SECTION:.MYSEC,RWS.")

should read

#pragma comment(linker, "/SECTION:.mysec,RWS")

Section names are case sensitive, sorry. You can also check that the section is created using 'dumpbin.exe /headers myfile.dll'.
I have tried the following but still not working:
#include "windows.h"
#pragma data_seg(".myseg")
int m_Var = 0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.MYSEC,RWS")

extern "C" __declspec(dllexport) void SetVar(int var);
extern "C" __declspec(dllexport) int GetVar();

BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID lpvReserved)
{
	return TRUE;
}

extern "C" __declspec(dllexport) void SetVar(int var)
{
	m_Var = var;
}

extern "C" __declspec(dllexport) int GetVar()
{
	return m_Var;
}

Open in new window

As I wrote, use

#pragma comment(linker, "/SECTION:.mysec,RWS")

instead. Section names are case sensitive.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, now I am trying to share a string variable with unknown size, I tried std::string but it doesn't work when I get it using GetVar().

In C# I want to be able to use something like the following:
[DllImport("dllfile.dll")]
public static extern string GetVar();

[DllImport("dllfile.dll")]
public static extern void SetVar(string var);

Open in new window

>>I tried std::string

That won't work - as I mentioned earlier: "don't expect to be able to store objects there, this will only work for POD types". A C-style char array will work, but no objects, for the reason that their method's addresses are potentially different for each process.
How can I get string data type variable to be shared in DLL?
As in the 1st example, use a 'char' buffer. Everything else is (unfortunately) prone to fail.
If I used a char buffer, can I get it as a string in C#? like:
string str = GetVar();

If you can post a sample, it would be better.

Thanks,
You coud do thatb(according to the 1st example) like

C++ DLL ("dllfile.dll"):  

#include <windows.h>
#include <tchar.h>

int GetString(LPSTR buf, int nMaxCount)
{
      TCHAR* pszText = SampleText;
      int nLen = _tcslen(pszText);

      if (nLen < nMaxCount)
      {
         _tcscpy(buf,pszText);

         return nLen;
      }

      return 0;
}

C#

using System.Text; // for StringBuilder
public class DllFileDll{
  [DllImport("dllfile.dll")]
  public static extern int GetString(StringBuilder buf, int nMaxCount);
}

StringBuilder cb = new StringBuilder(256);
ReturnStringDll.GetString(sb, sb.Capacity);

Open in new window

StringBuilder cb = new StringBuilder(256);

Open in new window

What in the case that I dont know the string size?
Hmm, you could implement a method that returns it beforehand ;o)
I guess you mean the same way as using GetWindowText() and GetWindowTextLength(), however is there is anyway to just use one method to get a string no matter what its length?
I am closing the question since I got the question partly answered and it's inactive.