Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please any one help me to pass data and images from a program in matlab to a program in c# . Both programs will be running simultaneously in same system or in a network.
I want something like remoting or memory share concept.
I searched a lot and most of the result I found is something like accessing whole matlab program as a DLL in C#, which is not feasible for me. I want both the programs to run independently and can communicate.
Posted
Comments
Nathan Minier 22-Aug-14 9:07am    
It depends largely on what you want to have happen, if the C# is a UI layer, or if it will be processing the data at all. You could put the C# in as a WCF service, and Matlab should be able to hook a system process like any other language (I would hope). You could expose interfaces on the service that would accept data transactions from the Matlab application and perform the pertinent processing.

You could even use such a service as an intermediary if you needed to.

Other than that, running the Matlab with a DLL interface and PInvoking is not a terrible route. It's really the same thing, but without the foundational support of doing it the other way around with WCF.

1 solution

I don't have a running example, but if the two applications are running on the same machine you can use functionality from the user32.dll assembly to get a 'screenshot' of the Matlab program window. This allows you to get the images. Getting the data would probably involve 'digging' in the control tree of the Matlab program to find the right controls containing the data. You can use Inspect to do this: http://msdn.microsoft.com/en-us/library/windows/desktop/dd318521(v=vs.85).aspx[^].

If you are on .NET 4.5, you can use the UI Automation functionality: http://msdn.microsoft.com/en-us/library/ms747327(v=vs.110).aspx[^].

Below is an example to get the images:
C#
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);

public static Bitmap PrintWindow(IntPtr hwnd)    
{       
    RECT rc;        
    GetWindowRect(hwnd, out rc);

    Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);        
    Graphics gfxBmp = Graphics.FromImage(bmp);        
    IntPtr hdcBitmap = gfxBmp.GetHdc();        

    PrintWindow(hwnd, hdcBitmap, 0);  

    gfxBmp.ReleaseHdc(hdcBitmap);               
    gfxBmp.Dispose(); 

    return bmp;   
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900