0

I have a part of code in C++ that I have to port in Java:

GetModuleFileName(AfxGetInstanceHandle(),strPath.GetBuffer(MAX_PATH),MAX_PATH - 1);
strPath.ReleaseBuffer();

I'm not really sure how to go about this. I do not have much experience in C++, especially with windows functions. The business logic of what I'm trying to create is this:

We want to create or edit an HTML file and save it as:

strPath+"CaseRerorts.htm"

The C++ side of the code uses:

ofstream ofile(strPath, ios::trunc|ios::app, filebuf::sh_read);

and then writes stuff on ofile.

If the htm file exists then override it with a new one.

I'm guessing I'm gonna have to use OutputStream and PrinstStream. The main problem however is that I do not know how to get the strPath to be the same as the one in the C++ version of the code. Any ideas? Thanks.

PentaKon
  • 4,139
  • 5
  • 43
  • 80
  • GetModuleFileName(AfxGetInstanceHandle(), ... ) just returns the fully qualified pathname of your current exectutable. The osream is then opened/created in the same directory (by the way, I'm not sure that this is a good idea, because the path were exes are stored should in principle be copy protected for standard users.) – Christophe Jul 16 '14 at 15:01
  • You should have a look at this existing answer: http://stackoverflow.com/a/7603444/3723423 and then this utility class: http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html. But I'd highly recommend to put the file not in the application directory but a user directory, or a directory defined by a configuration variable. – Christophe Jul 16 '14 at 15:09

2 Answers2

0

The C++ code appears to be invoking WinAPI functions that extract the "module filename" of the currently-running instance of the application. That does not translate directly into Java, so you have to determine what you will use as an analogue.

If the string must exactly match the one produced by some particular instance of the original application, then you can simply hard-code the value. If it must more generally replicate the spirit of the original then you're going to need to figure out what analogue to the executable file name you want to use ("java.exe" is probably not useful for this purpose). Perhaps the name of the class whose main() method is the application's starting point is what you would want. If your application is single-threaded then you can get that by generating and examining a stack trace, but otherwise you may need to make some kind of provision for retaining that information where you can later read it back.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

I ended up not having to create this functionality but from what I found out, a way to do what is asked is by using:

servletRequest.getSession().getServletContext().getRealPath("/");

This does not replicate the GetModuleFileName function but is something similar.

PentaKon
  • 4,139
  • 5
  • 43
  • 80