?
Solved

Previous Question Continued...

Posted on 2003-03-01
20
Medium Priority
?
655 Views
Last Modified: 2012-08-13
I have now identified where the file is and that it is definitely there when the function reaches that point. I have also checked the destination path as well and that seems ok. But it appears that the function still fails at that point. Any ideas what I can do now? Thanks,

Colin.
0
Comment
Question by:cbitservices
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
20 Comments
 
LVL 1

Author Comment

by:cbitservices
ID: 8048677
I have now got it to find and copy the file!!! However, if the file is already in the target location it still crashes. Is there a way of avoiding this. Because it prevents GP Install from continuing. Thanks,

Colin.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8048685
Pardon my ignorance, but I have no idea what you are talking about. Could you post the link to your previous question on this subject and or elaborate on your problem a little more?

Exceter
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8048803
Sorry,

The previous question which was effectively resolved was at: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20533520.html

I "continued" the question because the new problem was related to but carried on from the existing one. I therefore felt it fairest to post a new question.

The new problem is as follows:
I have now created a DLL with a procedure which copies a file. The procedure works when I run it OK. However, if the file exists at the target location the dll crashes. I need it to exit gracefully so that the program that calls it continues to run. So I need it to: copy the file if it doesn't exist at the target location, but otherwise to ignore it. Is there a dir function or similar with which I could determine whether or not the file currently exists?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:cbitservices
ID: 8049181
I have now got this far:

#include <windows.h>


extern "C" short _declspec(dllexport) _stdcall CopyAFile(HWND MainHandle, HWND DialogHandle, const char *pInstallDir,  const char *pSupportDir, const  char *pUser, const char *pCompany, const char *pSerial, const char *pAdditional)
{
const int DirStrLen = GetSystemDirectory(NULL,0);  // Find length of the directory string.
 char *DirStrPtr = new char[DirStrLen + 1]; // Allocate space for the string luss the NUL at the end.
 
 GetSystemDirectory(DirStrPtr,DirStrLen); // Get the directory string.

 string FilNam2 = pSupportDir;

 string FilNam3 = FilNam2 + "\\msvbvm60.dll";
 
 string FilNam = DirStrPtr;  // Copy to a real string.

 delete [] DirStrPtr;  // Delete theC-style string.

 char envvar[] = FilNam;    

 FilNam = FilNam + "\\msvbvm60.dll";  // Create filename from the path.
 
 //FilNam = "c:\\windows\\profiles\\colin blakemore\\desktop\\msvbvm60.dll";
 
 //MessageBox(NULL,FilNam3.c_str(),"The FilNam string",MB_OK);
 char pathbuffer[_MAX_PATH];
   char searchfile[] = "msvbvm60.dll";
   

   _searchenv( searchfile, envvar, pathbuffer );
   if( *pathbuffer != '\0' )
      return 1;
   else
     return CopyFile(FilNam3.c_str(),FilNam.c_str(),TRUE)!=0;
}

But unfortunately it produces a compile error as follows and I can't see what I need to do to overcome it. I believe it will work if I can sort out this last problem (well I hope so anyway!).
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8049187
Sorry, the compile error is: "error C2075: 'envvar' : array initialization needs curly braces"
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8049201
OK, I can see that FileNam is a character array and I can see that FilNam is a string type and that envvar is an array so I suppose I need to convert the string to a character array, but how do I do that?
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8049206
Sorry, what I meant was...

I can see that FilNam is a string type and that envvar is an array so I suppose I need to convert the string to a character array, but how do I do that?

Hope that clarifies what I am thinking.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8049363
It looks like you have a lot of "junk DNA" in that code.  Try explaining what you want to do.  It looks like perhaps you want to locate the particular file which might be in a directory that is on the PATH and then you want to copy that file into a different directory.  Is that the case?  If so, why not just say so?

-- Dan
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8049364
I have also  now realised that the searchenv function only searches through PATH, LIB environments, etc. So I don't think this is going to work. Is there another function I can use to find out whether a file is present, or a dir function which would do the same job?
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8049372
No, I want to copy a file to the windows system directory if it is not already there; I am not a C++ programmer and therefore don't know the functions available. Help would be appreciated.
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8049472
OK, you can forget it now, I've sorted it! I found the functions _findfirst and _chdir and with these together I have achieved my goal. The question can therefore be cancelled. Thank you.
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 8049533
>> I found the functions _findfirst and _chdir and with these together I
>> have achieved my goal. The question can therefore be cancelled.
>> Thank you.
It may be best to steer away from those.  Those are non-standard C++ library functions that Microsoft added to the C++ library in VC.  Any identifier that begins with an underscore and a lowercase letter is a non-standard addition to the library.   You can use those, but they are not very portable.  

A better solution is to use the windows API functions that do the same thing.  They are not portable to other OSs, however they are portable to other compilers running on windows.  (Its not a huge improvement, but it is one.)

You might look at FindFirstFile() and FindNextFile() in the Vc help   (note yuu may find multiple entries with these names, look at the one from the windows basic services.)

You might also look at SetCurrentDirectory(), although honestly most programs don't use this, and instead always specify absolute paths to files to be maintained.  Its a little "safer"  That way your code doesn't have to assume that the current directory won't change in the middle of some operation.
0
 
LVL 22

Expert Comment

by:nietod
ID: 8049548
>> I want to copy a file to the windows system directory if it is not already there;
Do you know about the GetWindowsDirectory() and GetSystemDirectory() functions.  They return the paths you are looking for.

And if you want to find just one, specific file, don't use FindFirstFile() etc.   That has a lot of overhead.  the traditional approach is to use GetFileAttributes().   If the file doesn't exist it returns 0xFFFFFFFF.  This isn't quite as straightforward, but it is less work---for the programmer and the OS.
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8049758
Hi Nietod,

I have accepted your comment as an answer because you have given me valuable further information which I did not know, and should oviously be aware of; thanks for that.

However, I have got it all working now and as long as it works on Windows '95, '98, ME, 2000 and XP. I will be very happy. Of course, I take your point and appreciate the assistance. I will also look at changing these to use the functions you suggest a bit later on, but at the moment, as time is of the essence, and I have wasted far too much just trying to get this working already! I feel I have to come back to it later. Perhaps I can contact you later if I need more assistance with this when I return to it (Of course this could be sooner than I think if I find I have problems with either of the systems I mentioned above; I still have to test on all of them yet; I have only tested on ME so far!). Thanks, again.
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8050046
OK, so I got too cocky! :-) It doesn't work on Win 95! I presume that this is due to the reasons you said. So it looks like I have to tackle the other functions as you suggested now. The function I have at present is:

#include <windows.h>
#include <direct.h>
#include <io.h>
#include <time.h>

extern "C" short _declspec(dllexport) _stdcall CopyAFile(HWND MainHandle, HWND DialogHandle, const char *pInstallDir,  const char *pSupportDir, const  char *pUser, const char *pCompany, const char *pSerial, const char *pAdditional)
{
const int DirStrLen = GetSystemDirectory(NULL,0);  // Find length of the directory string.
 char *DirStrPtr = new char[DirStrLen + 1]; // Allocate space for the string luss the NUL at the end.
 long hFile;
 struct _finddata_t c_file;

 GetSystemDirectory(DirStrPtr,DirStrLen); // Get the directory string.

 string FilNam2 = pSupportDir;

 string FilNam3 = FilNam2 + "\\msvbvm60.dll";
 
 string FilNam = DirStrPtr;  // Copy to a real string.

 _chdir( DirStrPtr);

 delete [] DirStrPtr;  // Delete theC-style string.

 FilNam = FilNam + "\\msvbvm60.dll";  // Create filename from the path.
 
 //FilNam = "c:\\windows\\profiles\\colin blakemore\\desktop\\msvbvm60.dll";
 

   if( (hFile = _findfirst( "msvbvm60.dll", &c_file )) == -1L )
   //{MessageBox(NULL,"File Not Found","The FilNam string",MB_OK);
        return CopyFile(FilNam3.c_str(),FilNam.c_str(),TRUE)!=0;
   //}
   else
   //{MessageBox(NULL,"File Found","The FilNam string",MB_OK);
          return 1;//}
}

and this works on Win ME and XP. However, when I tried it on '95 GP Install returned the error "Error loading extension"

I would be very grateful (and very generous with my points! :-) ) if you could tell me how to use the new functions in the above routine and thereby avoid me having to tackle the "cannot convert... " errors which I KNOW I will otherwise encounter! Thanks,

Kindest regards,

Colin.
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8050074
I have continued this question at: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20535080.html so that I can award more points for assistance. Thanks,

Colin.
0
 
LVL 22

Expert Comment

by:nietod
ID: 8050102
>> ) It doesn't work on Win 95! I presume that this is due to the reasons you said.
Probably not.

>> However, when I tried it on '95 GP Install returned the error "Error loading extension"
That sounds like it can't load the DLL?  maybe?

You could create a C++ program that tries to load the DLL (just use LoadLibrary() and see if it succeeds.  You don't have to be fancy.  You can do this in a console program and just print the results to the console.

somethig like

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
   HANDLE LibHnd = LoadLibrary("specify the path here.  Two backslashes for each one needed");

   if (LibHnd == 0)
     cout << "could not load"
   return 0;
};

If there is an error, tne try looking at the error code form GetLastError() and look up its description in the error look up under tools menu in VC.
0
 
LVL 1

Author Comment

by:cbitservices
ID: 8050211
I'm not sure how to do this. I am developing on an ME computer, and it works OK on there. When I try it on a '95 it fails to load. I don't have the development environment on the '95 machine because it is simply a test machine (which I have just done a reformat and installation of Windows '95 on).
0
 
LVL 22

Expert Comment

by:nietod
ID: 8051484
But you can (coudl have) written the above program on the ME computer and then copy it to windows 95.
0
 

Expert Comment

by:Hermy
ID: 11220502
He Colin,

I see that this question is old but I have the same problem as you now. I use Gp-install and a dll made in MSvisual C++ (winxp) to make a serial key checker. I have the same function declaration, and with dumpbin I can see if it is exported. In GP-install I  set the function and via a MessageBox I see that it loads the dll.

Now when you put nothing in you function code(just the messagebox) and return 1 the function also crashes!

In the gp-install help, they say:"when the function returns 1 the installer continues normally".
I don't get it.

Check my question

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21007268.html

Herman
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

771 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