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> W>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