Issues in using DLL files - c#

The matlab code below has been converted to a DLL file and it's being used in c# program in visual studio. While running the c# program it's showing an error as
undefined variable or function name imtool, error in trail.m
trail.m is the name of program in matlab.But when this "trail" is runned in matlab it's showing the desired output. Can you please find out a solution.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool
imtool close all; % Close all figure windows created by imtool.

The command imtool is not supported for use with MATLAB deployment products, including MATLAB Compiler and MATLAB Builder NE for .NET. Typically, most regular MATLAB and toolbox commands are supported, but not prebuilt GUIs such as imtool.
However, if the use of imtool is within a block of code surrounded by if (~isdeployed), then this shouldn't be a problem, as it will not be executed by the deployed component.
Your code snippet is incomplete, and doesn't have an end for the if. Can you confirm whether your imtool is within the if (~isdeployed) block? Or perhaps, since you're attempting to close some imtool windows, you have other uses of imtool within trail.m?

Related

Universal Windows Platform (UWP) C# app - How to run stand alone python inside your application

I am trying to find a way to call python from my UWP app. So far I have a .exe file that I have compiled from python using pyinstaller (www.pyinstaller.org/). This basically allows me to package up my python script as a standalone binary (ie: you don't need python to run it). This all works well and I can call my wrapped up python .exe via cmd.exe no problem:
$ process.exe -p "path\to\file"
$ Processing file: "path\to\file"...
$ Done.
So now I just need to call it from my UWP app - so I have added it to my application like so:
C# Project
Assets/process.exe
Frustratingly, I've not had much luck googling for answers to my problem - my attempted solutions so far have included:
Calling the "Assets/process.exe" directly from my app
Looked at "Launch an app and get results". I think this seams to be for external applications however... I certainly didn't get it going anyway.
Opening the cmd.exe (somehow) and calling my process.exe from there.
I'm not even sure if I'm trying to do this the correct way or not. Or if I have just not understood some of my findings. Or (fingers crossed) there is a simple solution to this I just don't know about and have somehow missed as I'm very new to UWP development and C#.
So any solutions/pointers here would be greatly appreciated thanks!!
UWP apps are 'sandboxed'; i.e. they have many security restrictions placed upon them to isolate them from the rest of Windows (like not being able to read/write to the Registry and not being able to directly access random files from the file system).
So there is no way to run an .exe (or any other executable) from your UWP app. If you have access to a StorageFile (say music, video or any other file format) then you can launch the file in the default program associated with that file type.

GhostScript PS to PDF with images only work during debug mode in Visual Studio

I'm trying to convert a ps file (word file with image) to pdf using Ghostscript.
Everything works fine when I'm debugging my code and just stepping thru it, It generates the pdf with the text,images and whatnot. But when I deploy the app using Visual Studio Setup Project, It does not work and gives me this error "An error occured when call to 'gsapi_new_instance' is made: -100."
Here's my command line arguments
var args = string.Format("-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=\"{1}\" -c save pop -f \"{0}\"", inputFile, #"C:\MedirefPrinter\converted\out.pdf");
Any idea why this isn't working? Thanks
Actual Code :
File Changed Handler
ShellCommand
Please excuse my noobness :)
Moved to an answer to allow more text.
There are three possible reasons for the error:
1) The 'instance' pointer is NULL. I can't see how this is ever possible with our executable as its a globally defined variable and the executable passes its address. This is a sanity check for people writing code against the Ghostscript API.
2) The application was unable to allocate sufficient memory for some internal structures. Again this seems unlikely as your system would have to be unreasonably short on memory.
3) The DLL instance count is already 1 or greater. This can happen if the DLL is shared between multiple processes. Unless you build the library with GS_THREADSAFE it isn't thread safe, and so you can't have multiple processes using the same instance of the DLL. I'd guess that this is your problem but obviously you haven't supplied a full set of code, so I don't know. If you are trying to run more than one copy of Ghostscript simultaneously, from the same directory, then you will get this error.
error -100 means 'something really bad happened so early on that I can't even tell you what it is'.
I very much doubt that the presence of images in the PostScript has any real impact, except that possibly it may slow the interpretation down enough to cause you to attempt to launch two processes.

COM Interop VB6 ActiveX EXE

In my VS2012 C# project, I am wiring a code that is using VC6 composed ActiveX EXE through its auto generated assembly equivalent that, I suppose, some plumbing is done with TLBIMP.exe when it is referenced in my C# project.
When the code is executed I see the ActiveX EXE being launched in a separate process and is terminated upon the termination of the C# code that it is referenced from.
It seems working as I expected, but I have hard time looking for documentation from which I can rest assure that it is the correct usage of ActiveX EXE in C# project.
Can anyone give some advice?
It is a good sign that you can see the process running. A very easy way to prove success and gain 100% confidence would be to add logging to your VC6 exe and examine the log to ensure it is behaving as you expect. For example, if your VC6 exe receives command line arguments, you could log what they are to ensure you are correctly executing it from your C# program. If you cannot change the VC6 exe, you are left with black box testing: With specific inputs, do you get the expected outputs? I suppose you could use fancier and more time consuming methods, but you might not need to if my suggestions are sufficient.

Converting a C# (Windows application) into commandline application?

I am looking to convert a C# (Windows platform application) into a commandline version.
The scenario is: I have implemented a C# (Windows application) in VS 2010. The output of this application is to generate a txt (log) file (in simple explanation).
Now the case is, there is one other application which need to use my this C# application, by calling my C# application from the command line at the run time.
My question is, how is it possible to convert an already existing C# application into commandline application, so that this C# application can be called from the calling (other) program? There is one input parameter which need to be passed on the commandline to my C# application. And then this C# application will process the data according to input parameter and then generate the output log(txt) file.
Added explanation
I am really impressed by the solutions here. Just a bit more expertise is required from readers. I want one application only to work as both commandline application as well Windows-application (forget to mention it before, sorry!), depending on the number of input parameter pass to the application. From this point of view, I have two options to implement it,
1) Make separate functions for both applications (commandline and windows-forms). Call them according to the input parameter pass. In each function implement the complete functionality of each application without disturbing (or going into the code of other application). Also I will be able to re-use 2 main functions, already built in windows-form application into my commandline application after some editing.
Disadvantage: This will make the code size nearly 50% more than case 2.
2) The second idea is same as describe by one of the expert here, to use the same application/functions for commandline as that of already built windows-form application. The only way to distinguish is to look at the input parameter pass, and decide accordingly whether to show the GUI interface or just use the commandline input (and do processing).
Disadvantage: This case will make the code bit messy and difficult to maintain/implement due to extra adding of check for number of input parameter decisions.
Which strategy should I follow for implementation?
Sure - just:
Create a new VS2010 command-line project
You'll now have a "main ()" (or, in MS-Land, "_tmain()") function instead of a root class.
Cut and paste the relevant code into "main()" (or into a function called by main (), or into a class created from main() - your choice).
Do a search-and-destroy mission to find anyplace where you're doing GUI input, and substitute command line parameters instead.
Parse your command line.
Voila! Done!
You don't have to convert it. Your application can stay as a Windows application. You simply need to handle command line arguments.
To get the command line arguments from ANYWHERE in the application, just use Environment.GetCommandLineArgs();
You want to get value from command line is not a good reason to convert winform app to console app. You may use,
string[] args = Environment.GetCommandLineArgs();
However you can change application type by opening project properties (right click on project name) and change the Output type.
Just don't show the GUI if you get paramater passed in, as when called from the other program.

debug matlab function called from c#

I call a *.m function from C# :
matlab.Feval("shoulderDetector", 3, out result, pic, colorPicR, colorPicG, colorPicB);
When I compile, this line opens the Command Window for matlab and sends the 4 variables(All variables are from live camera) to shoulderDetector.m that calls another 3 matlab funcitions... There is an error there "matrix exceeds it indices..." Is there a way knowing what's going on with data in matlab without printing everything in the CMD matlab window ?
If not, what is the best way detecting a bug ?
Unless you want to debug in software designed for debugging Matlab code, there is no way to do it. I would put debugging output to the console in the Matlab code where you think you can catch the issue, (e.g. near the problematic matrix) and output the indexes and any other information that might be helpful.
Unfortunately there is no good solution debugging solution for Matlab when you come across different programming languages.
I reccomend to replace your function temporarily with this one:
function varargout=genericSaveArgs(varargin)
varargout=cell(nargout,1);
callArgs=varargin;
save('callArgs.mat','callArgs');
end
which saves the parameters, then call your original function using:
cc=load('callArgs.mat')
[a,b,c]=shoulderDetector(cc.callArgs{:})

Categories