Two applications cummunication through DLL

Mohamed Abowarda
Mohamed Abowarda used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ramromconsultant

Commented:
What have you tried? Where are you stuck?
Mohamed AbowardaSenior Software Engineer

Author

Commented:
I am trying to use DLL to make communication between two applications that use the same DLL file, I haven't tried anything yet.
jkr
Top Expert 2012

Commented:
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.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Mohamed AbowardaSenior Software Engineer

Author

Commented:
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.
Top Expert 2012
Commented:
That's a Catch-22 - you need an initalizer for your variable, or that whole thing fails (sorry about forgetting to point that out). Try

#include "windows.h"
#pragma data_seg(".myseg")
int m_Var = 0; // <--- unfortunately, this one is important
#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

Mohamed AbowardaSenior Software Engineer

Author

Commented:
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

ramromconsultant

Commented:
Is there a reason for choosing DLL?

What is your "higher-level objective"?
jkr
Top Expert 2012

Commented:
>>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?
Mohamed AbowardaSenior Software Engineer

Author

Commented:
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.
jkr
Top Expert 2012

Commented:
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'.
Mohamed AbowardaSenior Software Engineer

Author

Commented:
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

jkr
Top Expert 2012

Commented:
As I wrote, use

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

instead. Section names are case sensitive.
Mohamed AbowardaSenior Software Engineer
Commented:
I figured out why the problem was occurring:

This line:
#pragma data_seg(".myseg");

Should be:
#pragma data_seg(".mysec");
Mohamed AbowardaSenior Software Engineer

Author

Commented:
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

jkr
Top Expert 2012

Commented:
>>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.
Mohamed AbowardaSenior Software Engineer

Author

Commented:
How can I get string data type variable to be shared in DLL?
jkr
Top Expert 2012

Commented:
As in the 1st example, use a 'char' buffer. Everything else is (unfortunately) prone to fail.
Mohamed AbowardaSenior Software Engineer

Author

Commented:
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,
jkr
Top Expert 2012

Commented:
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

Mohamed AbowardaSenior Software Engineer

Author

Commented:
StringBuilder cb = new StringBuilder(256);

Open in new window

What in the case that I dont know the string size?
jkr
Top Expert 2012

Commented:
Hmm, you could implement a method that returns it beforehand ;o)
Mohamed AbowardaSenior Software Engineer

Author

Commented:
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?
Mohamed AbowardaSenior Software Engineer

Author

Commented:
I am closing the question since I got the question partly answered and it's inactive.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial