473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to make repeated calls to a matlab DLL from C#?

2
Hi

I need to make repeated calls to a matlab DLL from C#. I have searched the websites and tried the various methods but somehow I couldn't get them to do what I need them to do. Initially I got memory access violation error, then invalid handle error using the GCHandle approach and finally for the codes below, I got garbage value returned from the DLL.

Could anyone please point me to the right way? Thanks.

Simplified codes:
============

class DllWrapper
{
...
[DllImport(@"libtest.dll")]
private static extern void mlfTest(int num, ref IntPtr theOutput, [In]IntPtr theInput);
...
public static double CallDLL(string input, IntPtr outVal)
{
...
double[] ans = new double[1];

//initialize libraries
...

//initialize arguments
IntPtr inVal = mxCreateString(input);

//call library function
mlfTest(1, ref outVal, inVal);

//get return value
Marshal.Copy(mxGetPr(outVal), ans, 0, 1);

//terminate libraries
...
return ans[0];
}
}

.......

static void Main(string[] args)
{
...

for (int i = 0; i < 3; i++)
{
IntPtr outPtr = Marshal.AllocHGlobal(100);
double retDouble = DllWrapper.CallDLL(path, outPtr);

Console.WriteLine("The return value is : " + retDouble);
if (outPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(outPtr);
outPtr = IntPtr.Zero;
}
}
}
Jul 31 '09 #1
1 2651
JGWong
2
Managed to solve the problem.
Aug 11 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mathias | last post by:
Dear NG, I currently ty to switch from matlab to python/scipy but have a lot of trouble with images. What I need is a function for subsequently displaying a number of 2D-matrices as an image. I...
4
by: dataangel | last post by:
I'm a student who's considering doing a project for a Machine Learning class on pathing (bots learning to run through a maze). The language primarily used by the class has been Matlab. I would...
3
by: Mohammed Smadi | last post by:
Hi; Does anyone know if we can call matlab for a python or bash script while feeding the matlab script some command line arguments? I have an interactive matlab script which i want to automate...
6
by: Lars Christiansen | last post by:
Hi! I am a master student in (geo)physics at the University of Copenhagen and part of a study group on C++ as a scientific programming language. I, and the other students in the group, have...
9
by: Carl | last post by:
I am desperately looking for a way to call Python from Matlab. I have become used to Python's rich syntax and large number of libraries, and feel ridiculously clumsy being stuck with Matlab's...
8
by: Allen | last post by:
Does anyone agree with me? If you have used Matlab, welcome to discuss it.
3
by: Ajith Menon | last post by:
Do any body know how to use the functions available in the MATLAB using C#? ( To be specific, functions COHERE used to find Coherence and TFE to calculate Transfer function are needed. These...
4
by: itcecsa | last post by:
Hi, I am implementing a small Python project, what I am going to do is to open Matlab and run some M-files, and get some output from Matlab command prompt. I have no idea how to open Matlab...
14
by: Luna Moon | last post by:
Dear all, Can C++/STL/Boost do the vectorized calculation as those in Matlab? For example, in the following code, what I really want to do is to send in a vector of u's. All other...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.