473,586 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Something like IniFile=Applica tion.Executable Path 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=Applica tion.Executable Path;

Is there something comparable in VC?

TIA

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


GetWindowsDirec tory().

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

When I try this:
Form1(void)

{

InitializeCompo nent();

TCHAR WinPath[_MAX_PATH+1]="";

GetWindowsDirec tory(WinPath,_M AX_PATH);

}

I get these compiler errors:

c:\junkfuck\For m1.h(51): error C3861: 'GetWindowsDire ctory': identifier not
found, even with argument-dependent lookup
c:\junkfuck\For m1.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******** ********@tk2msf tngp13.phx.gbl. ..
"Harry Whitehouse" <ha***@envmgr.c om> wrote in message
news:OA******** ******@TK2MSFTN GP14.phx.gbl...
I'm trying to obtain the load directory for my application,


GetModuleFileNa me().
as well as the Windows directory on the machine,


GetWindowsDirec tory().

Regards,
Will

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

{

InitializeCompo nent();

TCHAR WinPath[_MAX_PATH+1]="";

GetWindowsDirec tory(WinPath,_M AX_PATH);

}

I get these compiler errors:

c:\junkfuck\For m1.h(51): error C3861: 'GetWindowsDire ctory': identifier
not found, even with argument-dependent lookup
c:\junkfuck\For m1.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 ::InteropServic es;

[ DllImport("Kern el32.dll", EntryPoint="Get WindowsDirector yW",
CallingConventi on=CallingConve ntion::StdCall) ]
extern "C" int GetWindowsDirec tory(wchar_t *, unsigned int);

int _tmain()
{
wchar_t wszDir[260];

GetWindowsDirec tory(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=Applica tion.Executable Path;

Is there something comparable in VC?


It's available in MC++ too:

System::String^ iniFile =
System::Windows ::Forms::Applic ation::Executab lePath;

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.c om> wrote in message
news:eM******** ******@TK2MSFTN GP09.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
CallingConventi on=CallingConve ntion::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 interoperabilit y 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.c om> wrote in message
W> news:ej******** ******@TK2MSFTN GP12.phx.gbl...
W>
Hi Will!
W> Hello.
W> When I try this:
Form1(void)
{

InitializeCompo nent();

TCHAR WinPath[_MAX_PATH+1]="";

GetWindowsDirec tory(WinPath,_M AX_PATH);

}

I get these compiler errors:

c:\junkfuck\For m1.h(51): error C3861: 'GetWindowsDire ctory':
identifier
not found, even with argument-dependent lookup
c:\junkfuck\For m1.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 ::InteropServic es;
W> [ DllImport("Kern el32.dll", EntryPoint="Get WindowsDirector yW",
W> CallingConventi on=CallingConve ntion::StdCall) ]
W> extern "C" int GetWindowsDirec tory(wchar_t *, unsigned int);
W> int _tmain()
W> {
W> wchar_t wszDir[260];
W> GetWindowsDirec tory(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.gmai l.SPAM.com> wrote in message
news:13******** *************@m snews.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

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

Similar topics

1
3707
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
320
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 file on another machine when I deploy the application. In code, I am using the following lines to make the connection... temp =...
5
1455
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 ? - i prefer not to use the registry - also, is it possible to read the inifile before any form is loaded? I want to have a language specificaton...
13
3921
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 .NET creates i get the following error message: "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in VisioTimeline.exe
6
3143
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
1401
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 Timer1.Interval = 1 * 60 * 1000 Timer1.Start() End Sub Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As
14
17721
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 include the executable name. Thanks in Advance Laurence
1
1343
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 found"...Ok..We have set the File path and every thing alright...If i run the application in Debug mode , its running perfectly...But if i run the...
2
5318
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 db or save string to the db I don't want to have to copy the string to a file to use the WritePrivateProfileString and GetPrivateProfileString. Is...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.