Solved

Help with C  DLL pass string

Posted on 2010-11-26
8
520 Views
Last Modified: 2012-05-10

 Hi experts!

I’m totally new to MS VC 2008, but now I need to learn it on some level. I need a help with a call to external DLL function of Stradis decoder. My C project is OCX which call these functions. I have a working example from Stradis, however I’m not able to write my application ¿

Here you my code. Problem is with pass string – path and name of file. I get back error – File not found. File is existing in this part, so problem is with string pass.

For more sure I set value of sInitFile to be sure that problem is not due to conservation error when I pass this string to OCX from VB6.

// --------- My code with c OCX -----------------------
long CCPlayCtrl::Init(long lCard, long lVideoMode, LPCTSTR sInitFile)
{
      // TODO: Add your dispatch handler code here
      hDecoder = INVALID_STRADIS_HANDLE;
      STRADISDECODERSETTINGS phSettings;

      hDecoder = StradisDecoder_Create(lCard);
    StradisDecoder_DisplayErrorMessages( hDecoder, true);

      phSettings = StradisDecoderSettings_Create(VideoStandard_Auto/*(VideoStandard)lVideoMode*/);

  sInitFile="C:\\Saigon.mpg";

      int iRes = StradisDecoder_File_Open(hDecoder, sInitFile, phSettings); // RETURN ERROR _ FILE NOT FOUND

Bellow in the code section is a working example from SDK. This working example is compiled in VS C 2008 althod they are written in C6,it works and should be use like example what input string format demands this function. By the way, it was compiled under Unicode Solution Configuration, my example say stradis.dll can't be read while I try to compilie it under Debug Unicode and I compile it under Debug. Does this make error ???

For working code example ist with files which are opens are gets from Command String while start .exe.

Also in OCX this srting will be set from VB6. What type is more comfitable with VB6 ?



Declaration of function from MANUAL
------------------------------------
File_Open()
bool File_Open(char* pFileName, CStradisDecoderSettings* pSettings);

This function opens the requested disk file pointed to by pFileName and modifies the contents of the CStradisDecoderSettings pointed to by pSettings. If successfully opened, the file is bound to the pSettings class. 

This is from C code
-------------------
STRADIS_EXPORT int STRADISCALLMETHOD StradisDecoder_File_Open(STRADISDECODER hDecoder, LPCTSTR pFilename, STRADISDECODERSETTINGS hSettings);


Working code example. SHOULD BE USE TO SEE EXAMPLE OF WORKING STRING WHICH SHOULD BE PASS TO DLL
------------------------------------------------------------------------------------------------
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	STRADISDECODER hDecoder = INVALID_STRADIS_HANDLE;
	STRADISDECODERSETTINGS* phSettings = NULL;
	int i = 0;                   // Shared index variable.
	int nExpectedFiles = argc-1; // Number of filenames expected on the command line (based on argc).
	int nFiles = 0;              // Number of files actually opened.
	int nDecoderIndex = 0;       // Index of the decoder to use.

#ifdef USE_MFC
	// MFC is not necessary for this application to run.
	// Initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
		// TODO: change error code to suit your needs
		printf("Fatal Error: MFC initialization failed\n");
		return 1;
	}
#endif

	// Check for the correct number of arguments (at least one file).
	if(argc < 2) {
		printf("FilePlayC file1 <file2> <file3> ...\n");
		
		goto Exit;
	}

	// Create the decoder class for board 0 (first board)
	hDecoder = StradisDecoder_Create(nDecoderIndex);
	if(hDecoder == INVALID_STRADIS_HANDLE) {
		printf("Could not create decoder!\n");
		goto Exit;
	}
	if(StradisDecoder_IsValid(hDecoder) == false) {
		printf("Decoder invalid!\n");
		goto Exit;
	}
 
//	printf( hDecoder + "\n");

	// Initialize the Settings array and create a Settings handle for each file:
	phSettings = (STRADISDECODERSETTINGS*)malloc(sizeof(STRADISDECODERSETTINGS)*nExpectedFiles);
	memset(phSettings, 0x0, sizeof(STRADISDECODERSETTINGS)*nExpectedFiles);
	for(i = 0; i < nExpectedFiles; i++) {
		phSettings[i] = StradisDecoderSettings_Create(VideoStandard_Auto);
		if (phSettings[i] == INVALID_STRADIS_HANDLE) {
			printf("Could not create settings");
			goto Exit;
		}
	}

	// Open all of the files on the command line.
	for(i = 0; i < nExpectedFiles; i++) {
// -------------- HERE IS EXAMPLE ------------------
		TCHAR* szFilename = argv[i+1];
		if (StradisDecoder_File_Open(hDecoder, szFilename, phSettings[nFiles])) {
			_tprintf(_T("Could not open file %s: %s\n"), szFilename, Stradis_GetLastErrorText());
			continue;
		} else {
			nFiles++;
		}
	}

Open in new window

0
Comment
Question by:dvplayltd
8 Comments
 
LVL 5

Expert Comment

by:Xper4net
ID: 34216606
First, your string must be initialized as this :

  sInitFile=_T("C:\\Saigon.mpg") ;

If it doesn't work better, we could try to go further....
0
 

Author Comment

by:dvplayltd
ID: 34216631
To xPer4Net

I try this, result is same:
File not found

    sInitFile=_T("C:\\Saigon.mpg") ;

      int iRes = StradisDecoder_File_Open(hDecoder, sInitFile/*szFilename*/, phSettings);

What other to try ?
0
 
LVL 9

Assisted Solution

by:masheik
masheik earned 125 total points
ID: 34216862
can you try

strcpy(sInitFile,"C:\\Saigon.mpg");

or

sInitFile = strdup("C:\\Saigon.mpg");

.....code ....

free(sInitFile);

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:dvplayltd
ID: 34216933
I try

strcpy(sInitFile,"C:\\Saigon.mpg");

and get:
Error      1      error C2664: 'strcpy' : cannot convert parameter 1 from 'LPCTSTR' to 'char *'      d:\aadinserter\stardisocx\cplay\cplayctl.cpp      264      CPlay

Problem is not to set value, problem is with format of string. I think it demand Char pointer to first simbol which should be pass in array of chars ? Look def. of function:
bool File_Open(char* pFileName, CStradisDecoderSettings* pSettings);

Also, it should be unicode, does this make diffrances ?
0
 

Author Comment

by:dvplayltd
ID: 34216963
I mean, look this from example which works fine:

TCHAR* argv[]

      for(i = 0; i < nExpectedFiles; i++) {
            TCHAR* szFilename = argv[i+1];
            if (StradisDecoder_File_Open(hDecoder, szFilename, phSettings[nFiles])) {
                  _tprintf(_T("Could not open file %s: %s\n"), szFilename, Stradis_GetLastErrorText());
                  continue;
            } else {
                  nFiles++;
            }
      }

So my value should go to type TCHAR* argv[]
 May be I needo some loop from my string to goes to this char array ? Please offer example code.
0
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 125 total points
ID: 34217234
> So my value should go to type TCHAR* argv[]
> May be I needo some loop from my string to goes to this char array ? Please offer example code.

I think this is not correct - the loop you see in the sample code of your last comment IMO is used to open a number of files (passed via command line) one after another - each call to 'StradisDecoder_File_Open' gets passed a 'TCHAR*' as filename.

You should check if 'Stradis decoder's documentation has any information whether the filename has to be Unicode or not.

Further I would suggest to test it with another file path - the C:\ directory often has different permissions than other paths. Please try to create a path 'C:\temp', copy the 'saigon.mpg' to there and use 'C:\\temp\\saigon.mpg' in your code. Thus you can ensure it's not a permission problem ...

Further I think you should check the value returned by 'StradisDecoderSettings_Create' as it's done in the sample with:
if (phSettings[i] == INVALID_STRADIS_HANDLE)

Open in new window


ZOPPO
0
 
LVL 5

Accepted Solution

by:
Xper4net earned 250 total points
ID: 34217643
You said: "my example say stradis.dll can't be read while I try to compilie it under Debug Unicode"

I never encountered "dll can't be read" error message. Is it exactly that?

In the meantime, you can try the following :

TCHAR* pszFileName = __tcsdup(T_("C:\\Saigon.mpg")) ;

int iRes = StradisDecoder_File_Open(hDecoder, pszFileName , phSettings);
free(pszFileName) ;
0
 

Author Closing Comment

by:dvplayltd
ID: 34218361
10x to all! I solve question when I change Charter Set to Unicode (Project settings), then I pass directly value without any transofm.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question