Avatar of Brad Brett
Brad Brett
Flag 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
Editors IDEsCC++Microsoft Development

Avatar of undefined
Last Comment
Brad Brett

8/22/2022 - Mon
ramrom

What have you tried? Where are you stuck?
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.
jkr

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Brad Brett

ASKER
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
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Brad Brett

ASKER
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

ramrom

Is there a reason for choosing DLL?

What is your "higher-level objective"?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jkr

>>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?
Brad Brett

ASKER
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

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'.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Brad Brett

ASKER
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

As I wrote, use

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

instead. Section names are case sensitive.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Brad Brett

ASKER
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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jkr

>>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.
Brad Brett

ASKER
How can I get string data type variable to be shared in DLL?
jkr

As in the 1st example, use a 'char' buffer. Everything else is (unfortunately) prone to fail.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Brad Brett

ASKER
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

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

Brad Brett

ASKER
StringBuilder cb = new StringBuilder(256);

Open in new window

What in the case that I dont know the string size?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jkr

Hmm, you could implement a method that returns it beforehand ;o)
Brad Brett

ASKER
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?
Brad Brett

ASKER
I am closing the question since I got the question partly answered and it's inactive.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy