473,382 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Something like IniFile=Application.ExecutablePath in Visual C++?

I'm trying to obtain the load directory for my application, as well as the
Windows directory on the machine, but I don't know how this is accomplished
in VC++ .NET. In C# there is an Application object which provides this type
of information:

IniFile=Application.ExecutablePath;

Is there something comparable in VC?

TIA

Harry
Nov 17 '05 #1
11 2375
"Harry Whitehouse" <ha***@envmgr.com> wrote in message
news:OA**************@TK2MSFTNGP14.phx.gbl...
I'm trying to obtain the load directory for my application,
GetModuleFileName().
as well as the Windows directory on the machine,


GetWindowsDirectory().

Regards,
Will
Nov 17 '05 #2
Hi Will!

When I try this:
Form1(void)

{

InitializeComponent();

TCHAR WinPath[_MAX_PATH+1]="";

GetWindowsDirectory(WinPath,_MAX_PATH);

}

I get these compiler errors:

c:\junkfuck\Form1.h(51): error C3861: 'GetWindowsDirectory': identifier not
found, even with argument-dependent lookup
c:\junkfuck\Form1.h(270): error C2065: 'WindowsDir' : undeclared identifier
I've used this API call in straight windows before many times, but I can't
get it recognized in the .NET environment!

Best

Harry



"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Harry Whitehouse" <ha***@envmgr.com> wrote in message
news:OA**************@TK2MSFTNGP14.phx.gbl...
I'm trying to obtain the load directory for my application,


GetModuleFileName().
as well as the Windows directory on the machine,


GetWindowsDirectory().

Regards,
Will

Nov 17 '05 #3
"Harry Whitehouse" <ha***@envmgr.com> wrote in message
news:ej**************@TK2MSFTNGP12.phx.gbl...
Hi Will!
Hello.
When I try this:
Form1(void)

{

InitializeComponent();

TCHAR WinPath[_MAX_PATH+1]="";

GetWindowsDirectory(WinPath,_MAX_PATH);

}

I get these compiler errors:

c:\junkfuck\Form1.h(51): error C3861: 'GetWindowsDirectory': identifier
not found, even with argument-dependent lookup
c:\junkfuck\Form1.h(270): error C2065: 'WindowsDir' : undeclared
identifier
I've used this API call in straight windows before many times, but I can't
get it recognized in the .NET environment!


Right. That's because in the .Net environment, an application is expected to
use the framwork. You can "drop down" to the level of the API but it is some
work to do so. It wouldn't surprise me if there is a class in .Net with a
method to get what you want. I don't have time now to do the research.

As for using the API, you have two choices. One is Platform Invoke aka
P/Invoke. This is an example

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

[ DllImport("Kernel32.dll", EntryPoint="GetWindowsDirectoryW",
CallingConvention=CallingConvention::StdCall) ]
extern "C" int GetWindowsDirectory(wchar_t *, unsigned int);

int _tmain()
{
wchar_t wszDir[260];

GetWindowsDirectory(wszDir, sizeof(wszDir));

return 0;
}

Note that I hardcoded the size of the string. That's bad and only done here
as an expedient.

Another option is to use "it just works". IJW lets you have a managed caller
call into an unmanaged function. To do that, I'd suggest putting the native
API call in a module which does not use the framework and which is not
compiled with the /CLR option.

Regards,
Will
Nov 17 '05 #4
Harry Whitehouse wrote:
I'm trying to obtain the load directory for my application, as well
as the Windows directory on the machine, but I don't know how this is
accomplished in VC++ .NET. In C# there is an Application object
which provides this type of information:

IniFile=Application.ExecutablePath;

Is there something comparable in VC?


It's available in MC++ too:

System::String^ iniFile =
System::Windows::Forms::Application::ExecutablePat h;

Can't remember off the top of my head how to get the Windows directory in
..NET, but if you can do in C#, you can use the same method in MC++. See also
Will's post on interop.

--
Tim Robinson
MVP, Windows SDK
Nov 17 '05 #5
Bill -- That did it!

I had tried that with some C# syntax for the DLL call and it was crashing.
I see you need to add more verbage on the declaration and then it works!

Thanks so much!

Best

Harry
Nov 17 '05 #6
"Harry Whitehouse" <ha***@envmgr.com> wrote in message
news:eM**************@TK2MSFTNGP09.phx.gbl...
I had tried that with some C# syntax for the DLL call and it was crashing.
I see you need to add more verbage on the declaration and then it works!
You don't say what verbage. Here are a couple of tips. Virtually all of the
Win32 API uses the "standard" calling convention. C and C++ applications
tend to use a different one by default. So it is a good idea to specify the
calling convention, very nearly always
CallingConvention=CallingConvention::StdCall

In addition, Win32 API functions that take or return strings very nearly
always come in two variants - one whose name ends in 'A' and which takes and
returns ANSI characters and one whose name ends in 'W' and uses UNICODE. As
the .Net framework is all UNICODE, you are generally better off with the 'W'
variant.

Finally, go slow on the interoperability front. If you can find a .Net way
to do the same thing as a Win32 function as easily, prefer it instead.
Thanks so much!


You are welcome.

Regards,
Will
Nov 17 '05 #7
Hello William DePalo [MVP VC++],

Wouldn't you recommend IJW, and handling the marshling (via Marshal::XXX
methods) manually versus DllImport style ? I would think IJW performs better,
no ?

Thanks, RBischoff

W> "Harry Whitehouse" <ha***@envmgr.com> wrote in message
W> news:ej**************@TK2MSFTNGP12.phx.gbl...
W>
Hi Will!
W> Hello.
W> When I try this:
Form1(void)
{

InitializeComponent();

TCHAR WinPath[_MAX_PATH+1]="";

GetWindowsDirectory(WinPath,_MAX_PATH);

}

I get these compiler errors:

c:\junkfuck\Form1.h(51): error C3861: 'GetWindowsDirectory':
identifier
not found, even with argument-dependent lookup
c:\junkfuck\Form1.h(270): error C2065: 'WindowsDir' : undeclared
identifier
I've used this API call in straight windows before many times, but I
can't get it recognized in the .NET environment!

W> Right. That's because in the .Net environment, an application is
W> expected to use the framwork. You can "drop down" to the level of the
W> API but it is some work to do so. It wouldn't surprise me if there is
W> a class in .Net with a method to get what you want. I don't have time
W> now to do the research.
W>
W> As for using the API, you have two choices. One is Platform Invoke
W> aka P/Invoke. This is an example
W>
W> #include "stdafx.h"
W> #using <mscorlib.dll>
W> using namespace System;
W> using namespace System::Runtime::InteropServices;
W> [ DllImport("Kernel32.dll", EntryPoint="GetWindowsDirectoryW",
W> CallingConvention=CallingConvention::StdCall) ]
W> extern "C" int GetWindowsDirectory(wchar_t *, unsigned int);
W> int _tmain()
W> {
W> wchar_t wszDir[260];
W> GetWindowsDirectory(wszDir, sizeof(wszDir));
W>
W> return 0;
W> }
W> Note that I hardcoded the size of the string. That's bad and only
W> done here as an expedient.
W>
W> Another option is to use "it just works". IJW lets you have a managed
W> caller call into an unmanaged function. To do that, I'd suggest
W> putting the native API call in a module which does not use the
W> framework and which is not compiled with the /CLR option.
W>
W> Regards,
W> Will

Nov 17 '05 #8
"RBischoff" <ry****@NO.gmail.SPAM.com> wrote in message
news:13*********************@msnews.microsoft.com. ..
Wouldn't you recommend IJW, and handling the marshling (via Marshal::XXX
methods) manually versus DllImport style ? I would think IJW performs
better, no ?


Premature optimization is the root of much evil.

If the OP is going to call the function to get the Windows' directory once,
then I wouldn't worry _at all_ about which method is more performant.

If the OP is going to call some other unmanaged function alot, then I'd be
inclined to do the profiling rather than go off half-cocked in one directon
or other.

In fact, I'd be more inclined to investigate the possibility of minimizing
the transitions and of trying to do as much as possible per transition.

Regards,
Will
Nov 17 '05 #9
Hi Will!
If the OP is going to call some other unmanaged function alot, then I'd be
inclined to do the profiling rather than go off half-cocked in one
directon or other.

In fact, I'd be more inclined to investigate the possibility of minimizing
the transitions and of trying to do as much as possible per transition.


This is an excellent (indeed the best) suggestion. You definitely need to
think about how your code is going to be transitioning between managed and
unmanaged code, and preferably, design with this in mind to ensure it does
so in the best possible manners (this will not only help your performance,
it will make your debugging and development easier).

As for the original question, yes, IJW might be faster in many
circumstances, but it also comes with more work sometimes, as there pretty
much no safeguards and almost no automatic conversions of types, like
P/Invoke does.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #10
Hello Tomas,

You and Will make good points and I can cetrainly understand that IJW has
the possibility of a lot more work.
As someone mentioned previously the analogy of round trips to the database,
the same as trips from managed to unmanaged ;)

RBischoff
-----------------------------------------
http://msdn.microsoft.com/visualc/ (VC++ HOME)
http://www.mvps.org/vcfaq/ (C++ FAQ)
http://www.winterdom.com/mcppfaq/ (MC++ FAQ)
http://msdn.microsoft.com/visualc/whidbey/ (CLI)

TR> Hi Will!
TR>
If the OP is going to call some other unmanaged function alot, then
I'd be inclined to do the profiling rather than go off half-cocked in
one directon or other.

In fact, I'd be more inclined to investigate the possibility of
minimizing the transitions and of trying to do as much as possible
per transition.

TR> This is an excellent (indeed the best) suggestion. You definitely
TR> need to think about how your code is going to be transitioning
TR> between managed and unmanaged code, and preferably, design with this
TR> in mind to ensure it does so in the best possible manners (this will
TR> not only help your performance, it will make your debugging and
TR> development easier).
TR>
TR> As for the original question, yes, IJW might be faster in many
TR> circumstances, but it also comes with more work sometimes, as there
TR> pretty much no safeguards and almost no automatic conversions of
TR> types, like P/Invoke does.
TR>

Nov 17 '05 #11
Tim -- Good call! I figured there was something analogous!

Thanks to all for the help here!!

Best

Harry
Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jammersplace | last post by:
me and my friend made a media player and we need to know if there is anyway to make a visualisation for it.
3
by: David | last post by:
Hi, I have an application that connectes using oledb connection to an access database. The program works fine in debug and release mode on the developement machine but cannot locate the database...
5
by: alex | last post by:
Hi, I want to store user preferences in an inifile and load these at startup. - there may be different users accessing the same applicaton on a pc What is the best way of doing this in vb.net...
13
by: Lee Newson | last post by:
Hi, I have just written my first application using VB.NET. The app works fine when i am running it within .NET for debugging purposes, however when i try to run the app from the .exe file that...
6
by: beaker | last post by:
Hello, Is there a way to get the name of my application (as it appears on the list of processes running) at runtime? (Something to do with System.Reflection...?) Thanks, Gary
1
by: Dan Kimhi | last post by:
I'm unable to get the timer to fire the tick event. I use the following code: Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the application is started...
14
by: Larry | last post by:
How does one get the name of the executable that is running in VS.2005 using VB.net. I have looked at the my.application object but I can only find My.Application.Info.DirectoryPath which does not...
1
by: angelsherin | last post by:
hi friends ... i am working in vc++ MFC aplication...I have an application which reads value from the inifile...In that application, if the file is not found then we display a message "File not...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.