• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3951
  • Last Modified:

C# dll shared memory

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
yewnix
Asked:
yewnix
  • 5
  • 4
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
mac-willCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
yewnixAuthor Commented:
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
 
yewnixAuthor Commented:
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
 
yewnixAuthor Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
notice my suggestion can cause an exception if the resulting text is larger than the buffer capacity.
0
 
yewnixAuthor Commented:
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
 
yewnixAuthor Commented:
Changed solution, but I will award points anyways.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now