Solved

C# dll shared memory

Posted on 2008-10-01
10
3,812 Views
Last Modified: 2012-05-05
I'm a little confused here.

What I have is a 3rd party application. This 3rd party application allows you to create scripts and import DLLs to call DLL Functions.
What I need to do is shared data between this 3rd party application and my c# application.

ie: 3rd party application runs script which calls my dll function add_data.. at this point my c# application reads the data from the memory map, delete it, does some calculations, and then writes back to the memory map that the 3rd party application would then read(using a dll call)

Any help would be greatly appreciated.
0
Comment
Question by:yewnix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22618916
you can do it with unsafe code:

define some buffer somewhre as:
    byte[] buffer = new byte[someSize];

then to invoke your dll, create an unsafe function:
    public unsafe void yourMethod(byte[] buffer, other params....)
    {
              fixed (byte *b = buffer)
              {
                    CallYourDllMethodHere(b, buffer.Length, other params....);
              }
    }

your dll function should be properly declared with DlllImport attribute.
0
 
LVL 8

Expert Comment

by:mac-will
ID: 22621029
I would suggest not using "unsafe" code as in my limited experience it ends up causing more headaches than it is worth.

I would look into the Marshaling class.  You can allocated memory on the unmanaged heap and deallocated it later, you can pass a pointer to this memory and you can copy managed memory to and from this memory.

Here is a link:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx


If you post more details (like the method signatures of the DLL you are calling) maybe you can even set it up to let the marshaller do automatic marshaling.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22621086
unsafe is basically the same as working with native c++. if you use it with care, it will give you far more performance than the marshalling approach.
If you are a c++ developer, then use unsafe with confidence. If you are not familiarized with pointers, then use marshalling.
by the way, either using unsafe or marsalling, if your dll doesn't handle the buffer properly, you application will crash.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:yewnix
ID: 22627166
Okay, maybe i'm not explaining this well enough so I'll give it a better shot.
Please forgive me as I am new to C#, however I do understand programming logic.. just never done Shared Memory or much with DLLs.

Okay I know the 3rd party application(MetaTrader 4) isn't developed in c#. So going from managed to unmanaged code might be difficult.

Say in my MetaTrader script I import my dll.

Call the Dll(inside the script) SendDllDate(string message);
After call this inside the script I'm going to want to call another Dll function that will basical Wait for a response(event driven inside the dll)

My C# application will read the MemoryMap, process the message sent by the 3rd party application. Then set an event and data inside the MemoryMap

The 3rd party application will jump back out of the Dll function(because an event was set) then and return the data that was placed into the MemoryMap from my c# application.

Based on the data retrieved it will process data inside the script.

Maybe I'm going about this the wrong way.. but I think I might be on the right track.
0
 

Author Comment

by:yewnix
ID: 22635448
I basically want to build off of: (add a MemoryMap to store the data)

http://www.codeguru.com/csharp/csharp/cs_misc/dllsandexecutables/article.php/c14735/

In his c++ dll he does alot of converting.. is there a better way?

Also reference:
http://forum.mql4.com/6676/page2#30882

This shows the user using signed char* in the c++ dll.. However he doesn't pass back a string to the MetaTrader platform. He passes back a int. If this post is easier then converting(as in the first post) how can I change this to pass back the string to the MetaTrader platform?



0
 

Author Comment

by:yewnix
ID: 22636557
Okay.. so here is what I have and it works so far. don't know if its the best method so advice would be helpful.

MetaTrader Script
----------------------
#import "mt4wrapper.dll"
string ReturnMyName(string fname, string lname);

int start()
  {
      Comment(ReturnMyName("Foo", "Bar"));    
      return(0);
  }



C++ Wrapper DLL (mt4wrapper.dll)
----------------------

#include "stdafx.h"
#using <mt4test.dll>

__declspec(dllexport) signed char* __stdcall ReturnMyName(signed char* fname, signed char* lname)
{
      return mt4test::mt4test::ReturnMyName(fname, lname);
}


C# DLL(mt4test.dll)

namespace mt4test
{
    public unsafe static class mt4test
    {

        public static sbyte* ReturnMyName(sbyte* fname, sbyte* lname)
        {
            string name = "Your name is " + new string(fname) + " " + new string(lname);
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            byte[] r_name = enc.GetBytes(name);
            return (sbyte*)VarPtr(r_name);
        }

        static int VarPtr(object o)
        {
            System.Runtime.InteropServices.GCHandle GC =
            System.Runtime.InteropServices.GCHandle.Alloc(o,
            System.Runtime.InteropServices.GCHandleType.Pinned);
            int ret = GC.AddrOfPinnedObject().ToInt32();

            return ret;
        }
    }
}


This code actually works! When the MetaTrader script runs it does place in the comments section. "Your name is Foo Bar"

The only issue I see so far is I'm not unpinning the object after its being used.
Granted I still need to add all the MemoryMapping which ofcourse I'm new to that as well.
Any comments would be greatly appreciated!
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 22636989
well, there is not an explicit unallocation order, the c++ application won't handle the allocation automatically, I would suggest to use the following instead:

MetaTrader Script
----------------------
#import "mt4wrapper.dll"
string ReturnMyName(string fname, string lname);

int start()
  {
      signed char buffer[200] ;
      Comment(ReturnMyName("Foo", "Bar", buffer));    
      return(0);
  }



C++ Wrapper DLL (mt4wrapper.dll)
----------------------

#include "stdafx.h"
#using <mt4test.dll>

__declspec(dllexport) signed char* __stdcall ReturnMyName(signed char* fname, signed char* lname, signed char *buffer)
{
      return mt4test::mt4test::ReturnMyName(fname, lname, buffer);
}


C# DLL(mt4test.dll)

namespace mt4test
{
    public unsafe static class mt4test
    {
        public static sbyte* ReturnMyName(sbyte* fname, sbyte* lname, sbyte *buffer)
        {
            string name = "Your name is " + new string(fname) + " " + new string(lname);
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            byte[] r_name = enc.GetBytes(name);
            int i;
            for (i=0; i<r_name.Length; i++)
                   buffer[i] = (sbyte)r_name[i];
            buffer[i] = 0;
            return buffer;
        }
    }
}


0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22637001
notice my suggestion can cause an exception if the resulting text is larger than the buffer capacity.
0
 

Author Comment

by:yewnix
ID: 22637519
First off MetaTrader won't allow you to define a signed char buffer[200] ; inside the script

What I did instead was to just define a string buffer=""; inside the script and pass that.

However now after every cycle it doesn't reset the buffer and the comments keep extending out...
I even tried setting the buffer=""; everything the script runs and it still has extra data when it returns a value.
Something strange is going on here..

On a side note I talked to a friend who mention maybe writing a COM interface(DLL) and both applications using this dll to pass information...

I'm confused on the best way to do this


0
 

Author Comment

by:yewnix
ID: 22682280
Changed solution, but I will award points anyways.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question