I am trying to call VerQueryValue from a C# program. VerQueryValue takes as
one of its parameters a pointer to a pointer to an array of bytes, which it
uses to return a pointer to the required array. Now, I can call it like this:
byte* lpBuffer;
int length;
string subBlock = @"\StringFileIn fo\" + langCodePage +
@"\FileDescript ion";
result = VerQueryValue(b uffer, subBlock, &lpBuffer, &length);
but then I don't have a byte[] to pass to an ASCIEncoding object to decode.
What I really want to do is declare
byte[]* lplpBuffer
and then pass that to VerQueryValue, but C# apparently won't allow that.
(Oddly enough if I declare
byte[] lpBuffer;
I get an error "cannot convert from byte[]* to byte**", which is peculiar if
it doesn't recognise byte[]* as a type anyway)
Can anyone shed any light?
--
Dave 9 4810
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:E9******** *************** ***********@mic rosoft.com... I am trying to call VerQueryValue from a C# program. VerQueryValue takes as one of its parameters a pointer to a pointer to an array of bytes, which it uses to return a pointer to the required array. Now, I can call it like this: byte* lpBuffer; int length; string subBlock = @"\StringFileIn fo\" + langCodePage + @"\FileDescript ion"; result = VerQueryValue(b uffer, subBlock, &lpBuffer, &length); but then I don't have a byte[] to pass to an ASCIEncoding object to decode. What I really want to do is declare byte[]* lplpBuffer and then pass that to VerQueryValue, but C# apparently won't allow that. (Oddly enough if I declare byte[] lpBuffer; I get an error "cannot convert from byte[]* to byte**", which is peculiar if it doesn't recognise byte[]* as a type anyway) Can anyone shed any light? -- Dave
No need to use pointers, pass the byte[] by reference:
byte[] lpBuffer;
VerQueryValue(. ..., ref lpBuffer,...);
Willy.
'Fraid not! That gives "cannot convert from 'ref byte[]' to 'byte**'
--
Dave
"Willy Denoyette [MVP]" wrote: "Dave" <Da**@discussio ns.microsoft.co m> wrote in message news:E9******** *************** ***********@mic rosoft.com...I am trying to call VerQueryValue from a C# program. VerQueryValue takes as one of its parameters a pointer to a pointer to an array of bytes, which it uses to return a pointer to the required array. Now, I can call it like this: byte* lpBuffer; int length; string subBlock = @"\StringFileIn fo\" + langCodePage + @"\FileDescript ion"; result = VerQueryValue(b uffer, subBlock, &lpBuffer, &length); but then I don't have a byte[] to pass to an ASCIEncoding object to decode. What I really want to do is declare byte[]* lplpBuffer and then pass that to VerQueryValue, but C# apparently won't allow that. (Oddly enough if I declare byte[] lpBuffer; I get an error "cannot convert from byte[]* to byte**", which is peculiar if it doesn't recognise byte[]* as a type anyway) Can anyone shed any light? -- Dave
No need to use pointers, pass the byte[] by reference:
byte[] lpBuffer; VerQueryValue(. ..., ref lpBuffer,...);
Willy.
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:1F******** *************** ***********@mic rosoft.com... 'Fraid not! That gives "cannot convert from 'ref byte[]' to 'byte**' --
How do you have the function declared?
David
..
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:1F******** *************** ***********@mic rosoft.com... 'Fraid not! That gives "cannot convert from 'ref byte[]' to 'byte**' -- Dave
"Willy Denoyette [MVP]" wrote:
Please show us your VerQueryValue function declaration as declared in C,
describe the function semantics, who's allocating the buffers C# or the
unmanaged function, why the double redirection?
Willy.
You need to declare the function like the following:
[DllImport("What Ever.dll")]
public unsafe static extern void VerQueryValue (byte** buffer);
In the method you need to put the following:
//allocate memory on the heap
byte* dataArray = (byte*)Marshal. AllocCoTaskMem( 20);
byte** dataArrayArray = &dataArray;
//call the unmanaged function you import from dll file
VerQueryValue(d ataArrayArray);
//do what every you want here, before free the memory
//free the memory
Marshal.FreeCoT askMem((IntPtr) dataArray);
hope it helps,
Ivan Wong
"Dave" wrote: I am trying to call VerQueryValue from a C# program. VerQueryValue takes as one of its parameters a pointer to a pointer to an array of bytes, which it uses to return a pointer to the required array. Now, I can call it like this: byte* lpBuffer; int length; string subBlock = @"\StringFileIn fo\" + langCodePage + @"\FileDescript ion"; result = VerQueryValue(b uffer, subBlock, &lpBuffer, &length); but then I don't have a byte[] to pass to an ASCIEncoding object to decode. What I really want to do is declare byte[]* lplpBuffer and then pass that to VerQueryValue, but C# apparently won't allow that. (Oddly enough if I declare byte[] lpBuffer; I get an error "cannot convert from byte[]* to byte**", which is peculiar if it doesn't recognise byte[]* as a type anyway) Can anyone shed any light? -- Dave
Hi guys, thanks for helping on this.
I have just found an MSDN tutorial at http://msdn.microsoft.com/library/de...detutorial.asp
which explains how to do this. I can't say I fully understand how it gets
around the all the convolutions of pointers to pointers etc, but I've used it
and it works. Thanks to you all for looking at it. Hopefully this thread will
help someone else.
--
Dave
"Willy Denoyette [MVP]" wrote: .. "Dave" <Da**@discussio ns.microsoft.co m> wrote in message news:1F******** *************** ***********@mic rosoft.com... 'Fraid not! That gives "cannot convert from 'ref byte[]' to 'byte**' -- Dave
"Willy Denoyette [MVP]" wrote:
Please show us your VerQueryValue function declaration as declared in C, describe the function semantics, who's allocating the buffers C# or the unmanaged function, why the double redirection?
Willy.
Dave, the samples in the tutorial illustrates you how to use unsafe
constructs, but as I said before YOU don't need unsafe constructs to achieve
the same if you don't want to. Use unsafe only when you realy need to (IMO
never in C#).
Here is the same sample not using unsafe constructs:
...
public class Win32Imports
{
[DllImport("vers ion.dll")]
public static extern bool GetFileVersionI nfo (string sFileName,
int handle, int size, byte[] infoBuffer);
[DllImport("vers ion.dll")]
public static extern int GetFileVersionI nfoSize (string sFileName,
out int handle);
[DllImport("vers ion.dll")]
public static extern bool VerQueryValue (byte[] pBlock,
string pSubBlock, out string pValue, out uint len);
[DllImport("vers ion.dll")]
public static extern bool VerQueryValue (byte[] pBlock,
string pSubBlock, out IntPtr pValue, out uint len);
}
public class C
{
public static int Main ()
{
try
{
int handle = 0;
// Figure out how much version info there is:
int size =
Win32Imports.Ge tFileVersionInf oSize("printver sion.exe",
out handle);
if (size == 0) return -1;
byte[] buffer = new byte[size];
if (!Win32Imports. GetFileVersionI nfo("printversi on.exe",
handle, size, buffer))
{
Console.WriteLi ne("Failed to query file version
information.");
return 1;
}
IntPtr subBlock = IntPtr.Zero;
uint len = 0;
// Get the locale info from the version info:
if (!Win32Imports. VerQueryValue (buffer,
@"\VarFileInfo\ Translation", out subBlock, out len))
{
Console.WriteLi ne("Failed to query version
information.");
return 1;
}
int p = (int)subBlock;
int i1 = Marshal.ReadInt 16((IntPtr)p);
p+=2;
int i2 = Marshal.ReadInt 16((IntPtr)p);
string sb = String.Format(" {0:X4}{1:X4}",i 1, i2);
string spv = @"\StringFileIn fo\" + sb +
@"\ProductVersi on";
// Get the ProductVersion value for this program:
string versionInfo;
if (!Win32Imports. VerQueryValue (buffer, spv, out
versionInfo, out len))
{
Console.WriteLi ne("Failed to query version
information.");
return 1;
}
Console.WriteLi ne ("ProductVersio n == {0}", versionInfo);
}
catch (Exception e)
{
Console.WriteLi ne ("Caught unexpected exception " +
e.Message);
}
return 0;
}
}
Willy.
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:88******** *************** ***********@mic rosoft.com... Hi guys, thanks for helping on this. I have just found an MSDN tutorial at http://msdn.microsoft.com/library/de...detutorial.asp which explains how to do this. I can't say I fully understand how it gets around the all the convolutions of pointers to pointers etc, but I've used it and it works. Thanks to you all for looking at it. Hopefully this thread will help someone else. -- Dave
"Willy Denoyette [MVP]" wrote:
.. "Dave" <Da**@discussio ns.microsoft.co m> wrote in message news:1F******** *************** ***********@mic rosoft.com... > 'Fraid not! That gives "cannot convert from 'ref byte[]' to 'byte**' > -- > Dave > > > "Willy Denoyette [MVP]" wrote: >
Please show us your VerQueryValue function declaration as declared in C, describe the function semantics, who's allocating the buffers C# or the unmanaged function, why the double redirection?
Willy.
Hi Willy.
yes, thanks for that. Once I got the code from the sample working I started
taking out the unsafe constructs and eventually lo and behold they were all
gone and it still worked. I'm an experienced C++ programmer but still new to
C#. Actually I'm quite impressed that it can make all those calls without
resorting to pointers.
--
Dave
"Willy Denoyette [MVP]" wrote: Dave, the samples in the tutorial illustrates you how to use unsafe constructs, but as I said before YOU don't need unsafe constructs to achieve the same if you don't want to. Use unsafe only when you realy need to (IMO never in C#).
Here is the same sample not using unsafe constructs:
... public class Win32Imports { [DllImport("vers ion.dll")] public static extern bool GetFileVersionI nfo (string sFileName, int handle, int size, byte[] infoBuffer); [DllImport("vers ion.dll")] public static extern int GetFileVersionI nfoSize (string sFileName, out int handle); [DllImport("vers ion.dll")] public static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out string pValue, out uint len);
[DllImport("vers ion.dll")] public static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out IntPtr pValue, out uint len); }
public class C { public static int Main () { try { int handle = 0; // Figure out how much version info there is: int size = Win32Imports.Ge tFileVersionInf oSize("printver sion.exe", out handle);
if (size == 0) return -1;
byte[] buffer = new byte[size];
if (!Win32Imports. GetFileVersionI nfo("printversi on.exe", handle, size, buffer)) { Console.WriteLi ne("Failed to query file version information."); return 1; } IntPtr subBlock = IntPtr.Zero; uint len = 0; // Get the locale info from the version info: if (!Win32Imports. VerQueryValue (buffer, @"\VarFileInfo\ Translation", out subBlock, out len)) { Console.WriteLi ne("Failed to query version information."); return 1; } int p = (int)subBlock; int i1 = Marshal.ReadInt 16((IntPtr)p); p+=2; int i2 = Marshal.ReadInt 16((IntPtr)p); string sb = String.Format(" {0:X4}{1:X4}",i 1, i2); string spv = @"\StringFileIn fo\" + sb + @"\ProductVersi on";
// Get the ProductVersion value for this program: string versionInfo;
if (!Win32Imports. VerQueryValue (buffer, spv, out versionInfo, out len)) { Console.WriteLi ne("Failed to query version information."); return 1; }
Console.WriteLi ne ("ProductVersio n == {0}", versionInfo); } catch (Exception e) { Console.WriteLi ne ("Caught unexpected exception " + e.Message); }
return 0; } } Willy.
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message news:88******** *************** ***********@mic rosoft.com... Hi guys, thanks for helping on this. I have just found an MSDN tutorial at http://msdn.microsoft.com/library/de...detutorial.asp which explains how to do this. I can't say I fully understand how it gets around the all the convolutions of pointers to pointers etc, but I've used it and it works. Thanks to you all for looking at it. Hopefully this thread will help someone else. -- Dave
"Willy Denoyette [MVP]" wrote:
.. "Dave" <Da**@discussio ns.microsoft.co m> wrote in message news:1F******** *************** ***********@mic rosoft.com... > 'Fraid not! That gives "cannot convert from 'ref byte[]' to 'byte**' > -- > Dave > > > "Willy Denoyette [MVP]" wrote: >
Please show us your VerQueryValue function declaration as declared in C, describe the function semantics, who's allocating the buffers C# or the unmanaged function, why the double redirection?
Willy.
Dave,
All pointer stuff is taken care of by the interop layer in the CLR
(PInvoke), but rest assured they still exist, be it under the covers.
Willy.
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:19******** *************** ***********@mic rosoft.com... Hi Willy. yes, thanks for that. Once I got the code from the sample working I started taking out the unsafe constructs and eventually lo and behold they were all gone and it still worked. I'm an experienced C++ programmer but still new to C#. Actually I'm quite impressed that it can make all those calls without resorting to pointers. -- Dave
"Willy Denoyette [MVP]" wrote:
Dave, the samples in the tutorial illustrates you how to use unsafe constructs, but as I said before YOU don't need unsafe constructs to achieve the same if you don't want to. Use unsafe only when you realy need to (IMO never in C#).
Here is the same sample not using unsafe constructs:
... public class Win32Imports { [DllImport("vers ion.dll")] public static extern bool GetFileVersionI nfo (string sFileName, int handle, int size, byte[] infoBuffer); [DllImport("vers ion.dll")] public static extern int GetFileVersionI nfoSize (string sFileName, out int handle); [DllImport("vers ion.dll")] public static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out string pValue, out uint len);
[DllImport("vers ion.dll")] public static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out IntPtr pValue, out uint len); }
public class C { public static int Main () { try { int handle = 0; // Figure out how much version info there is: int size =
Win32Imports.Ge tFileVersionInf oSize("printver sion.exe", out handle);
if (size == 0) return -1;
byte[] buffer = new byte[size];
if (!Win32Imports. GetFileVersionI nfo("printversi on.exe", handle, size, buffer)) { Console.WriteLi ne("Failed to query file version information."); return 1; } IntPtr subBlock = IntPtr.Zero; uint len = 0; // Get the locale info from the version info: if (!Win32Imports. VerQueryValue (buffer, @"\VarFileInfo\ Translation", out subBlock, out len)) { Console.WriteLi ne("Failed to query version information."); return 1; } int p = (int)subBlock; int i1 = Marshal.ReadInt 16((IntPtr)p); p+=2; int i2 = Marshal.ReadInt 16((IntPtr)p); string sb = String.Format(" {0:X4}{1:X4}",i 1, i2); string spv = @"\StringFileIn fo\" + sb + @"\ProductVersi on";
// Get the ProductVersion value for this program: string versionInfo;
if (!Win32Imports. VerQueryValue (buffer, spv, out versionInfo, out len)) { Console.WriteLi ne("Failed to query version information."); return 1; }
Console.WriteLi ne ("ProductVersio n == {0}", versionInfo); } catch (Exception e) { Console.WriteLi ne ("Caught unexpected exception " + e.Message); }
return 0; } } Willy.
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message news:88******** *************** ***********@mic rosoft.com... > Hi guys, thanks for helping on this. > I have just found an MSDN tutorial at > http://msdn.microsoft.com/library/de...detutorial.asp > which explains how to do this. I can't say I fully understand how it > gets > around the all the convolutions of pointers to pointers etc, but I've > used > it > and it works. Thanks to you all for looking at it. Hopefully this > thread > will > help someone else. > -- > Dave > > > "Willy Denoyette [MVP]" wrote: > >> >> .. >> "Dave" <Da**@discussio ns.microsoft.co m> wrote in message >> news:1F******** *************** ***********@mic rosoft.com... >> > 'Fraid not! That gives "cannot convert from 'ref byte[]' to 'byte**' >> > -- >> > Dave >> > >> > >> > "Willy Denoyette [MVP]" wrote: >> > >> >> Please show us your VerQueryValue function declaration as declared in >> C, >> describe the function semantics, who's allocating the buffers C# or >> the >> unmanaged function, why the double redirection? >> >> Willy. >> >> >> >> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of code...
TCHAR myArray;
DoStuff(myArray);
|
by: Bryan Parkoff |
last post by:
I want to allocate pointer array into memory so pointer array contains
ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks
like below for example.
unsigned char* A = new unsigned char ;
It has only one pointer contains 1,000 bytes. How can I do this to
create pointer list like below.
unsigned char** B = new (unsigned char*) ; // Pointer List contains
|
by: Richard A. Lowe |
last post by:
I'm successfully using SendInput to emulate mouse events
for a legacy app, delcared like so:
public extern static int SendInput( int theCount, ref
INPUT pInputs, int theSize );
From the MSDN docs, SendInput appears to take an array of
inputs:
|
by: Herrcho |
last post by:
in K&R Chapter 6.3
it mentions two methods to calculate NKEYS.
and points out the first one which is to terminate the list of
initializers with a null pointer, then loop along keytab until the end
is found is less efficient than using sizeof operator , since size of
the array is completely determined at compile time.
i don't quite understand this. Could anyone explain to me in detail ?
|
by: Wynand Winterbach |
last post by:
I think every C programmer can relate to the frustrations that malloc
allocated arrays bring. In particular, I've always found the fact that
the size of an array must be stored separately to be a nightmare.
There are of course many solutions, but they all end up forcing you to
abandon the array syntax in favour of macros or functions.
Now I have two questions - one is historical, and the other practical.
1.) Surely malloc (and...
| |
by: **Developer** |
last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that
only the "A" version would work correctly. Anyone have a suggestion of why
the "W" version would not work correctly?
One reason is that the ByRef or ByVal does not show by InteliSence so the
"p" would help there.
------
Secondly, I'd like to be consistent with the parameter names. I'd like to
|
by: CJ |
last post by:
Functions can accept "argv like" variable definitions, i.e. foo(int
argc, char *argv), which was defined/initialized in the C start up
stubs, but we can't declare one for our own use.
Example:
foo(char *args) { /* implementation excluded }; // ok
int main(int argc,char *argv)
{
|
by: ottawajn |
last post by:
Hi, There,
I want to declare a two-dimension array by
"float coeftemp1 ;" in Dev c++. It doesn't work.
But, if I change it to "float coeftemp1 ;".
It works. Could any one know the reason?
Thank you,
|
by: hstagni |
last post by:
Where can I find a library to created text-based windows applications?
Im looking for a library that can make windows and buttons inside
console.. Many old apps were make like this, i guess
____________________________________
| |
| ------------------ |
| | BUTTON | |
| ...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |