Writing a MATLAB file from a DLL called by LabVIEW - c++

After testing my code alone, I decided to incorporate it into the project finally. The issue is, when LabVIEW 2010 (SP1, 64-bit) loads the custom DLL, it walks the dependencies and finds that it needs tbb.dll eventually. Well, as far as I can tell, LabVIEW uses its own version of tbb.dll. And its version is missing the entry point ?throw_exception_v4#internal#tbb##YAXW4exception_id#12##Z. I ran the function before separately, and it worked fine. It would appear this is not an unheard of LabVIEW error which hasn't been addressed.
Since this is a dependency of a third party library, I can either not use the MATLAB libraries and make everything from scratch, or I can force LabVIEW to load the working version of tbb.dll, which appears to mean copying the DLL into the Windows folder. Neither of these are usable solutions. Additionally, we do not have the license for Mathscript. Any ideas?
Just in case I did something wrong when I modified their example, a simplified version of my code is as follows:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "mat.h"
#include "createMatFile.h"
export "C" int createMatFile(const char *filename, int* dataArray, int count)
{
MATFile *pmat;
mxArray *data;
mwSize dims[2] = {1,1};
//Open the file to write to
pmat = matOpen(filename, "w");
if (pmat == NULL) {
printf("Error creating file %s\n", filename);
printf("(Do you have write permission in this directory?)\n");
return(EXIT_FAILURE);
}
//Convert data to double
double* dataDouble = new double[count];
for(int i=0; i<count; i++)
{
dataDouble[i] = (double)data[i];
}
//Populate the mxArrays
dataArray = mxCreateDoubleMatrix(1,count,mxREAL);
memcpy((void *)(mxGetPr(dataArray)), (void *)dataDouble, sizeof(*dataDouble)*count);
//Put the shape struct in the .mat file
int status = matPutVariable(pmat, "data", dataArray);
if (status != 0) {
printf("%s : Error using matPutVariable on line %d\n", __FILE__, __LINE__);
return(EXIT_FAILURE);
}
//Clean up
delete [] dataDouble;
mxDestroyArray(dataArray);
//Close the file to write to
if (matClose(pmat) != 0) {
printf("Error closing file %s\n",filename);
return(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
The .h file is just the prototype of the function.

Because LabVIEW installs its own version of tbb.dll into the windows path, and loads that, the only way to fix it is to copy the new tbb.dll in the system32 folder. This is not a good solution, so the only answer possible appears to be either:
Use this system32 hack which will probably cause other issues down the road.
Create some hack to dynamically load the proper DLL in a separate process with nothing loaded from LabVIEW and call the MATLAB DLL from that process then return the value computed to the LabVIEW process. Since my program only writes data to a file, it doesn't have to return anything back to LabVIEW, simplifying this choice.
Or just stop using MATLAB and LabVIEW in conjunction.

Related

lodepng, stb_image. Nothing works in my system for image loading in C++

I'm trying to load pngs or bmps into my programs. But none of the libraries I found around the web works. I have no idea why but I always get "incorrect PNG signature, it's no PNG or corrupted" for EVERY png when using lodepng. "unknown pixel format" for EVERY bmp when using SDL_loadBMP. "unknown image type" for every png when using stb_image.
I can't load anything. Maybe there is something wrong with my system ? I'm using OSX Yosemite. Here is the code.
#include <iostream>
#include <string>
#define STB_IMAGE_IMPLEMENTATION
#include <lodepng.h>
#include <stb_image.h>
using namespace std;
int main (){
string name = "res/img_test.png";
const char * cstr = name.c_str();
//lodepng
unsigned char *buffer;
unsigned int w,h;
int result = lodepng_decode32_file(&buffer, &w, &h, cstr);
cout << lodepng_error_text(result) << endl;
//stb_image
int x, y, comp;
FILE *f = fopen(cstr, "rb");
unsigned char *res;
res = stbi_load_from_file(f,&x,&y,&comp,0);
fclose(f);
cout << stbi_failure_reason() << endl;
return 0;
}
I'm using latest cmake to build this with gcc. Any recommendatation is appreciated but consider this. I've tried many files (generated by me or grabbed from internet). Tested same files with other users of the these libraries. Their code worked and mine didn't.
Edit:
Here's the source with complete cmake project >> github.com/onatbas/png_load_test.git
I solved it! Thank you everyone for trying to help.
It wasn't a code issue, afterall. It is a configuration issue. My cmake script damages the pngs and bmps when trying to copy them into destination folder. The code is fine.

Errors while reading a text file

I have a problem with reading a text file in c++ and I can't figure out what is wrong. I need to read few complex numbers from file and do some calculations using matlab functions. So before that I compiled matlab library and included them to my project. I tested them and it works correctly. But when I added a few lines of code for reading data from file I get some errors:
Here is my code:
#include "spectrum.h"
#include <iostream>
#include <fstream>
#include <complex>
using namespace std;
int main(){
mclInitializeApplication(NULL,0);
spectrumInitialize();
cout<<"Initialization success"<<endl;
double input[4] = {10,20,30,40};
mxArray *x_ptr, *x_ptr1;
mxArray *y_ptr=NULL;
mxArray *y_ptr1=NULL;
double *y;
double *y1;
complex<double> com[256];
ifstream myfile("dep.txt");
double a = NULL;
for(int i=0; i<256;i++)
{
myfile >> a;
com[i].real(a);
myfile >> a;
com[i].imag(a);
}
myfile.close();
x_ptr1 = mxCreateDoubleScalar(256);
x_ptr = mxCreateDoubleMatrix(1,256,mxCOMPLEX);
memcpy((void *)mxGetPr(x_ptr), (void *) com,256*sizeof(complex<double>));
mlfSpectrum_slice(1,&y_ptr,x_ptr,x_ptr1);
y = (double*)mxGetPr(y_ptr);
cout<<"Data: "<<input<<endl;
cout<<"Result: "<<*y<<endl;
mxDestroyArray(x_ptr);
mxDestroyArray(y_ptr);
spectrumTerminate();
mclTerminateApplication();
return 0;
}
I tried to use fstream and ifstream too. Anybody knows what I'm doing wrong?
By the way, I'm using Windows 7 32bit, Visual studio 2012 and Matlab R2012b.
You have a linkage problem. The linker can't find the function CrtDbgReport. This is a Microsoft debug function. It is all about writing debug messages using OutputDebugString API. Microsoft lib is kernel32.lib - so have you linked with that? Specifically it is looking for the Unicode version, that's the W on the end: CrtDbgReportW. So you must have set a Unicode build somewhere. ie #define UNICODE.
Is your lib maybe NOT using Unicode.
Another possibility is you are linking against debug matlab lib but you are building a release version of your program. Or vice versa.

Getting system model name using C++ on AIX

I need to (via a C++ program) get the system model name. Platform is AIX 5.3. Basically I need to get the same information as "uname -M" command. e.g. IBM, 1234-E4A
I tried using the uname() but could not get this information.
Any help/suggestions appreciated.
Thanks
I'm surprised that you don't appear to be getting this information with uname. What is the content of the machine field in the utsname structure when you call uname? That's where it should be:
#include <stdio.h>
#include <errno.h>
#include <sys/utsname.h>
int main (void) {
struct utsname uts;
if (uname (&uts) == -1)
printf ("Error, errno = %d\n", errno);
else
printf ("%s\n", uts.machine);
return 0;
}
Actually, based on the doco from IBM, they treat machine slightly differently.
subroutine uname utsname.machine: 32-bit unique system identifier represented as a hexadecimal character string, such as "0123454C".
So that's the system ID rather than a machine type.
As a last resort, you can always do the kludgy system ("uname -M >/tmp/myfile");
and then read in myfile with the standard C I/O stuff.
However, though that may be necessary is standard C, we can do better in AIX, since we have popen available to us. This allows us to run the command and examine its output without the hassle of creating and reading a temporary file.
Something like this:
#include <stdio.h>
int main (void) {
int ch;
FILE *fin = popen ("uname -M", "r");
if (fin != NULL) {
while ((ch = fgetc (fin)) != EOF) {
putchar (ch);
}
fclose (fin);
}
return 0;
}

Change Windows 7 Wallpaper in C++

I saw someone at school who had a program that was supposed to change the background, and log you out (so the change would take effect) but it only worked on his computer, and he "lost" the source code.
I have been looking online for a while for code that would do that, so I could experiment with it.
I found this code (C++)
#include <windows.h>
int main()
{
SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, (PVOID)"image.jpg", SPIF_UPDATEINIFILE );
}
which sort of worked..
It worked twice with an image that was located in the same folder, and I tested two images, and after the second one it just stopped working.
I can't for the life of me figure out why it stopped working (I have tried multiple file extensions, so I know that isn't the issue).
I am working in Dev-C++, if that makes a difference to anyone.
Also, I would like to be able to modify the code to use a url instead of a local image...would that be possible?
Thanks!
Edit: If I change image.jpg to image.bmp it changes every time..But that means that it would only work with *.bmp? I had it working with a jpeg before.
#include <windows.h>
int main()
{
int i;
for(i=0;;i++)
{
Sleep(800);
if(i%2==0)
{
const wchar_t *filenm = L"C:\\Pictures\\image1.jpg"; //ADDRESS of first image
bool isWallSet=SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0,(void*)filenm,SPIF_UPDATEINIFILE);
}
else
{
const wchar_t *filenm = L"C:\\Pictures\\image2.jpg"; //ADDRESS of second image
bool isWallSet=SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0,(void*)filenm,SPIF_UPDATEINIFILE);
}
}
return 0;
}

How to hide using C\C++ with Show hidden Files and Folder enabled

I would like to know how to programmatically hide a file but making the file or folder still hidden with Show hidden files and folders enabled from the Tools->Folder options of Explorer.
I am pretty sure this is not possible as it would be a security vulnerability (programs could place unknown files on your hard drive that you couldn't see or delete).
Even Windows system files are visible.
There is NO user mode API to make files hidden from 'show hidden files', and it's a good thing to.
The only way to do this is to get your code to run in the kernel. The rootkit that Sony "accidently" installed on user machines a couple years ago when they were trying to prevent CDs from being rippable could do it. But there is no legitimate use for the ability to hide files from system administrators and power users.
You want the SetFileAttributes function available by #include'ing Windows.h: http://msdn.microsoft.com/en-us/library/aa365535%28VS.85%29.aspx
In code:
BOOL result = SetFileAttributes(L"c:\path\to\file", FILE_ATTRIBUTE_HIDDEN);
As for keeping a file hidden from the "show hidden files" option, that's much more difficult and I can't think of a legitimate reason to do it - the only programs that do are rootkits for nefarious purposes.
Use a file system filter driver. But since you have to ask - just don't do it. Not trying to be rude here, it's just that that is a task that is very hard to get right.
As many have said before, there's no straightforward way to fully 'hide' a file like that.
If you can accept not truly hiding the file but merely obfuscating it, you can always embed it inside a dummy file. Take the file you want to hide, build a container file to hold it, and name that dummy file with a random name. For example, you could place the real file's filename starting at offset 512 of the dummy file and place the file's contents starting at offset 1024, inserting 64 bytes of random data every 1KB. Pad the end with empty space out to the nearest multiple of 4KB, fill the empty space with random bytes, and generate a random sequence of characters to use for a filename. Now, you can "hide" the file while it's still visible in the filesystem.
However, that's merely "security by obscurity" and can be defeated by a clever attacker with a hex editor.
If you're simply trying to make sure a file isn't visible to the casual filesystem browser, you can always compress the file, encrypt it, and randomize the filename. If you need to be able to access/execute the file as-is while it is "hidden", then you're probably (hopefully) out of luck.
This program is used to show all hidden files and folder in your system drive and it can run well even system has affected by virus and worms, usually, some kinds of the virus change your files and folders in a hidden state, this script will help you to view your files and folders again. The main feature is without installation it can play.
Please find the source code from my tech blog - http://www.algonuts.info/how-to-show-hidden-files-and-folders-using-c.html
#include<iostream>
#include<conio.h>
#include<dirent.h>
#include<dir.h>
#include<process.h>
#include<string.h>
#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<sys/stat.h>
struct ffblk vfile;
unsigned long int udata;
char ch,present[MAXPATH];
int next_directory(char *);
void scan_directory(char *);
char base[]="X:\\";
using namespace std;
int main(int account,char *arg[],char *env[])
{
clrscr();
getcwd(present,MAXPATH);
DIR *dir;
struct dirent *temp;
cout<<"\nWelcome to Unhidden for identify the hidden files";
cout<<"\n\nEnter drive:";
cin>>ch;
base[0]=ch;
if((dir = opendir(base)) == NULL)
{
clrscr();
cout<<"\nError : Derive not found ";
getch();
exit(0);
}
scan_directory(base);
while((temp = readdir(dir)) != NULL)
{
char *directory = (char *) malloc(3+strlen(temp->d_name)+1);
strcpy(directory,base);
strcat(directory,temp->d_name);
next_directory(directory);
free(directory);
}
closedir(dir);
clrscr();
cout<<"\nSystem: Successfully Unhidden it";
sleep(3);
return 0;
}
int next_directory(char *path)
{
int count=0;
DIR *dirtemp;
char *hold,*temp;
struct dirent *ptemp;
hold=path;
if ((dirtemp = opendir(path)) != NULL)
scan_directory(path);
else
return 0;
while((ptemp = readdir(dirtemp)) != NULL)
{
char *directory = (char *) malloc(1+strlen(ptemp->d_name)+1);
directory[0]='\\';
strcpy(directory+1,ptemp->d_name);
if(directory[1]!='\.')
{
count=strlen(hold);
temp = (char *) malloc(strlen(hold)+strlen(directory)+1);
strcpy(temp,hold);
strcat(temp,directory);
free(directory);
if(opendir(temp)!=NULL)
next_directory(temp);
temp[count]='\0';
hold=temp;
}
else
free(directory);
}
closedir(dirtemp);
return 0;
}
void scan_directory(char *tempo)
{
cout<<"\n"<<tempo;
unsigned count;
if(present[0]==tempo[0])
chdir(tempo);
else
{
setdisk(tempo[0]-65);
chdir(tempo);
}
udata = findfirst("*.*",&vfile,0x02);
while(!udata)
{
_dos_getfileattr(vfile.ff_name,&count);
if (count & _A_HIDDEN)
{
_dos_getfileattr(vfile.ff_name,&count);
count=count & 248;
_dos_setfileattr(vfile.ff_name,count);
}
udata=findnext(&vfile);
}
if(present[0]==tempo[0])
system("cd\\");
chdir(present);
}