|
I thought about porting the code but since the source in C is around 15000 lines filled of complex memory and mathematical operations I have no doubt that doing porting without mistakes will be quite a challenge.
|
|
|
|
|
I didn't say it would be easy, just that it was what you had to do!
You really SHOULD do it this way because, as you said above, you want to develop your own variant of JPEG. Well, you're going to have to understand the code in order to update it with your implementation changes. There's no better way than rewriting it in your own language.
|
|
|
|
|
Hi,
I've created a c-shared matlab library, which I want to use in my solution.
Using DllImport I can initialize the matlab environment, initialize my matlab model
and send parameters to this dll.
All works wel..., except I can't get the results!
In matlab, I've got a simple (test) function which returns a fixed value: 1234.
The created library has the following .h file:
extern LIB_Matlab_Test_C_API bool MW_CALL_CONV mlfMatlab_test(int nargout
, mxArray** steps
, mxArray* test_string
, mxArray* nog_een_test_string);
This is how I interface to the dll:
[DllImport("Matlab_Test.dll", EntryPoint = "mlfMatlab_test")]
static extern bool Matlab_Test(
[Out][MarshalAs(UnmanagedType.I4)]int nargout,
IntPtr return_value,
[In][MarshalAs(UnmanagedType.LPStr)]string myString,
[In][MarshalAs(UnmanagedType.LPStr)]string mySecondString);
And finally, calling the function:
int nargout = 1;
IntPtr pbuf = Marshal.AllocHGlobal(256);
Matlab_Test(nargout, pbuf,"dummy string", "yet another dummy string");
int sOut = Marshal.ReadInt32(pbuf);
Console.WriteLine("\t\tsucceeded! Value = {0}", sOut);
Marshal.FreeHGlobal(pbuf);
The value of sOut always remains zero
Does anyone have an idea of what I'm doing wrong here?
-- Stupidity should be painfull --
|
|
|
|
|
Hi,
I'm not sure what is wrong, however there is an easy way to get native string results; this is my recipe, and it works without any Marshal attributes:
const int CAPACITY=1000;
StringBuilder sb=new StringBuilder(CAPACITY);
NativeFunction(sb, sb.Capacity, ...);
string result=sb.ToString();
[DllImport...] static extern void NativeFunction(string textBuffer, int textBufferSize, ...);
and the native code looks like
void NativeFunction(char* textBuffer, int textBufferSize, ...) {...}
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hey jan,
Is your problem is solved!!
I am stuck with the same problem, I am also getting the return value as 0.
Plz reply
|
|
|
|
|
Hi Save-Tigers,
I managed to get it to work, thanks to the support of Matlab
although they mentioned several times I should buy the .net compiler
from them..
This is (part of my) code:
1) initialize matlab environment:
[DllImport("mclmcrrt710.dll", EntryPoint = "mclInitializeApplication_proxy")]
static extern bool InitiateMatlabApplication(string options, Int32 count);
call it from your code:
bool result = InitiateMatlabApplication(null, 0);
2) initialize your matlab code:
[DllImport("MatlabModel.dll", EntryPoint = "MatlabModelInitialize")]
static extern bool InitiateMatlabModel();
Note that MatlabModel must be replaced by whatever you named it
call it from your code:
bool result = InitiateMatlabModel();
3) Passing string arguments to Matlab:
[DllImport("MatlabModel.dll", EntryPoint = "mlfInitializeProject")]
static extern bool InitializeProject(IntPtr RootDir,
IntPtr Name);
Where RootDir and Name are strings. Use mxcreate_string_proxy from Matlab
to convert to matlab-format:
[DllImport("mclmcrrt710.dll", EntryPoint = "mxCreateString_proxy", CharSet = CharSet.Ansi)]
static extern IntPtr CreateString([In][MarshalAs(UnmanagedType.LPStr)] string myString);
Use this in your code:
IntPtr ptrLocation = CreateString(strProjectLocation);
IntPtr ptrName = CreateString(Settings.ProjectName);
Now, use the pointers above to pass strings to matlab:
result = InitializeProject(ptrLocation, ptrName);
4) Retrieving double argument from Matlab:
I've created a function which returns a double, nothing more:
[DllImport("MatlabModel.dll", EntryPoint = "mlfGetNofSteps")]
To use it in your code:
IntPtr result_ptr = IntPtr.Zero;
bool result = GetNofSteps(1, ref result_ptr)
To convert the pointer to an integer:
[DllImport("mclmcrrt710.dll", EntryPoint = "mxGetScalar_proxy")]
static extern double GetScalar(IntPtr mxArray);
So, to convert the pointer to value:
int NumberOfSteps = Convert.ToInt32(GetScalar(result_ptr));
5) Retrieving from and sending arguments to Matlab:
I've created a function which returns some matrices and uses a double as variable:
[DllImport("MatlabModel.dll", EntryPoint = "mlfmyFunction")]
static extern bool myFunction(int nargout,
ref IntPtr ptrResultMatrix1,
ref IntPtr ptrResultMatrix2,
ref IntPtr ptrResultMatrix3,
IntPtr ptrSTep);
First, to create pointers to matrices Matlab can use as output parameters, use this:
[DllImport("mclmcrrt710.dll", EntryPoint = "mxCreateDoubleMatrix_700_proxy")]
static extern IntPtr CreateDoubleMatrix(int cols, int rows, int flags);
Use the above function to declare pointers to matrices in which Matlab will return the values:
IntPtr ptrResultMatrix1 = CreateDoubleMatrix(10, 1, 0);
IntPtr ptrResultMatrix2 = CreateDoubleMatrix(10, 1, 0);
IntPtr ptrResultMatrix3 = CreateDoubleMatrix(10, 1, 0);
Next, create matrix to use as input parameter:
double[] value = new double[1];
value[0] = Convert.ToDouble(DayStep);
IntPtr ptrStapDag = CreateDoubleMatrix(1, 1, 0);
IntPtr value_ptr = GetPtr(ptrStep);
Marshal.Copy(value, 0, value_ptr, 1);
Now the pointers are set, call the created function:
bool result = myFunction(3, ref ptrResultMatrix1, ref ptrResultMatrix2,
ref ptrResultMatrix3, ptrStep);
Notice the 'ref' for output parameters!
I hope the above sample code helps you out.
Maybe I should write an article on codeproject, explaining it in detail.
-- Stupidity should be painfull --
|
|
|
|
|
Hey thanks JAN,
That was realy realy a very good help......
thanks thanks a lot for this
Reg,
Deep
Happy coding
|
|
|
|
|
hi
can you help explaning how you create the dll,
using mcc?
what is the line you write when you compile m file?
i am asking this because i wrote:
mcc -W cpplib:TestFunction -T link:lib TestFunction.m
and in the export finctions i got mlxTestFunction instead of "mlfTestFunction"
thanks
|
|
|
|
|
Hi Nizar,
I didn't create the dll using the command line,
instead I used deploytool.
I selected the Microsoft compiler, which creates
both mlx as well as mlf functions.
-- Stupidity should be painfull --
|
|
|
|
|
hi
can you help explaning how you create the dll,
using mcc?
what is the line you write when you compile m file?
i am asking this because i wrote:
mcc -W cpplib:TestFunction -T link:lib TestFunction.m
and in the export finctions i got mlxTestFunction instead of "mlfTestFunction"
thanks
|
|
|
|
|
hi
i try the same way you discriped here but when it get to call myFunction if failed.
any idea?
bool result1 = InitiateMatlabApplication(null, 0); // pass ok
bool result2 = TestFunctionInitialize(); // Pass ok
bool result3 = mlxTestFunction(); // failed
bool result4 = TestFunctionTerminate();
thanks
|
|
|
|
|
I didn't use the mlx function, instead I used the mlf functions.
The InitiateMatlabApplication, TestFunctionInitialize and TestFunctionTerminate
are default created by Matlab, soo it is likely they will pass.
-- Stupidity should be painfull --
|
|
|
|
|
I use the test runner of Visual Studio 2008, and I'm trying to get 100% code coverage with my unit tests in which I have almost succeeded. However the last couple percent are a bitch.
I have some code in a base class that throws an error if some conditions are not met. This code is marked as only partially tested (beige). I know for a fact that this code is exercised several times during the tests. However there are some descendant classes that cannot cause condition that will lead to the execution of this particular section of code. Is this the reason I get the incomplete coverage result, and how do I cope with it?
|
|
|
|
|
K.v.S. wrote: Is this the reason I get the incomplete coverage result, and how do I cope with it?
That is plausible.
You could add tests for the base class itself.
Or you could add another derived class that does need the specific base class code, and test it.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hello i need to set a query string to null or empty after it had a value can any body assist me??
|
|
|
|
|
Try to explain that again please.
If you want to set a string to null, which probably is not want you want, you can do...
string s = "something";
s = null;
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
I have a query string that is used in a page.
i sent this url from pop up page lets say
window.opener.location.href = window.opener.location.href +" 'Id=1'";
but after i get the Id
request.queryString["Id"]
i want it to be string.empty or null i simply want to remove it or assign to it a value so i will not check it again.
|
|
|
|
|
Well that makes more sense, but I cannot help you I'm afraid, try posting in the Web Development forum instead next time.
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
QueryString collection is readonly, it can not be modified.
Coding C#
www.excitetemplate.com
|
|
|
|
|
Hi guys,
I need some help please. I have a parent form (mdi) where a context menu item click opens up a child form, that child form takes a long time to load and so I wish to somehow allow the user to be able to click on a different context menu item click while child form A is being loaded.
Please advice of a way of doing this.
Thank you in advance.
Sameer
|
|
|
|
|
Your initial thought of thread is correct. However, you have to do all GUI stuff on the GUI thread, including the showing of the form. What you could do instead of this is move the code which is invoked when you show child form A onto a different thread. Just make sure that you use the Invoke method of child form A if you need to actually fiddle around with the controls.
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Thank you for your prompt response. Is there any way you could give me an example. I tried using a background worker but that doesn't work because of MTA restrictions. I was getting a dragdrop.... error. I tried initiating a regular thread and that is not working either because of what you suggested "GUI stuff on the GUI thread" All I am doing is GUI stuff, here is the code in my button click on mdi to open child form A
try
{
this.Cursor = AdvancedCursors.Create(Path.Combine(Application.StartupPath, "wagtail.ani"));
if (frm2 != null)
{
frm2.Close();
}
frm2 = new frmGrid2(m_ADBCnn, TechnicianstoolStripMenuItem1.Text, frm1);
frm2.CellClicked += new CellClickedEventHandler(frm2_CellClicked);
if (frm1 != null)
{
this.frm1.CellClicked += new CellClickedEventHandler(this.frm2.frm1_CellClicked);
this.frm2.CellClicked += new CellClickedEventHandler(this.frm1.frm2_CellClicked);
}
frm2.Show();
frm2.MdiParent = this;
this.VerticalTiletoolStripMenuItem1.PerformClick();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Sameer
|
|
|
|
|
There are three things which could cause the delay:
this.Cursor = AdvancedCursors.Create(Path.Combine(Application.StartupPath, "wagtail.ani"));
frm2 = new frmGrid2(m_ADBCnn, TechnicianstoolStripMenuItem1.Text, frm1);
this.VerticalTiletoolStripMenuItem1.PerformClick();
Which of those lines causes the delay? You can find this out by stepping through the code and seeing which line seems to take longer to execute.
When you know which line takes the longest time to execute, put it into a separate thread.
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
It's definately frm2 = new frmGrid2(m_ADBCnn, TechnicianstoolStripMenuItem1.Text, frm1); and frm2.show(). The part that takes long is the loading of the child form because it pulls quite a bit of data so it opens it but then the grid in child form is blank until all data is pulled.
Sameer
Sameer
|
|
|
|
|
What you need to do is get the data in a separate thread. In the GUI thread, set the grid's data source to your table. Then start the separate thread which adds the data to the data source. The grid should get updated automatically.
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|