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 = @"\StringFileInfo\" + langCodePage +
@"\FileDescription";
result = VerQueryValue(buffer, 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 4784
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:E9**********************************@microsof t.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 = @"\StringFileInfo\" + langCodePage + @"\FileDescription"; result = VerQueryValue(buffer, 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**@discussions.microsoft.com> wrote in message news:E9**********************************@microsof t.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 = @"\StringFileInfo\" + langCodePage + @"\FileDescription"; result = VerQueryValue(buffer, 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**@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com... 'Fraid not! That gives "cannot convert from 'ref byte[]' to 'byte**' --
How do you have the function declared?
David
..
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.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("WhatEver.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(dataArrayArray);
//do what every you want here, before free the memory
//free the memory
Marshal.FreeCoTaskMem((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 = @"\StringFileInfo\" + langCodePage + @"\FileDescription"; result = VerQueryValue(buffer, 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**@discussions.microsoft.com> wrote in message news:1F**********************************@microsof t.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("version.dll")]
public static extern bool GetFileVersionInfo (string sFileName,
int handle, int size, byte[] infoBuffer);
[DllImport("version.dll")]
public static extern int GetFileVersionInfoSize (string sFileName,
out int handle);
[DllImport("version.dll")]
public static extern bool VerQueryValue (byte[] pBlock,
string pSubBlock, out string pValue, out uint len);
[DllImport("version.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.GetFileVersionInfoSize("printversion. exe",
out handle);
if (size == 0) return -1;
byte[] buffer = new byte[size];
if (!Win32Imports.GetFileVersionInfo("printversion.ex e",
handle, size, buffer))
{
Console.WriteLine("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.WriteLine("Failed to query version
information.");
return 1;
}
int p = (int)subBlock;
int i1 = Marshal.ReadInt16((IntPtr)p);
p+=2;
int i2 = Marshal.ReadInt16((IntPtr)p);
string sb = String.Format("{0:X4}{1:X4}",i1, i2);
string spv = @"\StringFileInfo\" + sb +
@"\ProductVersion";
// Get the ProductVersion value for this program:
string versionInfo;
if (!Win32Imports.VerQueryValue (buffer, spv, out
versionInfo, out len))
{
Console.WriteLine("Failed to query version
information.");
return 1;
}
Console.WriteLine ("ProductVersion == {0}", versionInfo);
}
catch (Exception e)
{
Console.WriteLine ("Caught unexpected exception " +
e.Message);
}
return 0;
}
}
Willy.
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:88**********************************@microsof t.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**@discussions.microsoft.com> wrote in message news:1F**********************************@microsof t.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("version.dll")] public static extern bool GetFileVersionInfo (string sFileName, int handle, int size, byte[] infoBuffer); [DllImport("version.dll")] public static extern int GetFileVersionInfoSize (string sFileName, out int handle); [DllImport("version.dll")] public static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out string pValue, out uint len);
[DllImport("version.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.GetFileVersionInfoSize("printversion. exe", out handle);
if (size == 0) return -1;
byte[] buffer = new byte[size];
if (!Win32Imports.GetFileVersionInfo("printversion.ex e", handle, size, buffer)) { Console.WriteLine("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.WriteLine("Failed to query version information."); return 1; } int p = (int)subBlock; int i1 = Marshal.ReadInt16((IntPtr)p); p+=2; int i2 = Marshal.ReadInt16((IntPtr)p); string sb = String.Format("{0:X4}{1:X4}",i1, i2); string spv = @"\StringFileInfo\" + sb + @"\ProductVersion";
// Get the ProductVersion value for this program: string versionInfo;
if (!Win32Imports.VerQueryValue (buffer, spv, out versionInfo, out len)) { Console.WriteLine("Failed to query version information."); return 1; }
Console.WriteLine ("ProductVersion == {0}", versionInfo); } catch (Exception e) { Console.WriteLine ("Caught unexpected exception " + e.Message); }
return 0; } } Willy.
"Dave" <Da**@discussions.microsoft.com> wrote in message news:88**********************************@microsof t.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**@discussions.microsoft.com> wrote in message news:1F**********************************@microsof t.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**@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.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("version.dll")] public static extern bool GetFileVersionInfo (string sFileName, int handle, int size, byte[] infoBuffer); [DllImport("version.dll")] public static extern int GetFileVersionInfoSize (string sFileName, out int handle); [DllImport("version.dll")] public static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out string pValue, out uint len);
[DllImport("version.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.GetFileVersionInfoSize("printversion. exe", out handle);
if (size == 0) return -1;
byte[] buffer = new byte[size];
if (!Win32Imports.GetFileVersionInfo("printversion.ex e", handle, size, buffer)) { Console.WriteLine("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.WriteLine("Failed to query version information."); return 1; } int p = (int)subBlock; int i1 = Marshal.ReadInt16((IntPtr)p); p+=2; int i2 = Marshal.ReadInt16((IntPtr)p); string sb = String.Format("{0:X4}{1:X4}",i1, i2); string spv = @"\StringFileInfo\" + sb + @"\ProductVersion";
// Get the ProductVersion value for this program: string versionInfo;
if (!Win32Imports.VerQueryValue (buffer, spv, out versionInfo, out len)) { Console.WriteLine("Failed to query version information."); return 1; }
Console.WriteLine ("ProductVersion == {0}", versionInfo); } catch (Exception e) { Console.WriteLine ("Caught unexpected exception " + e.Message); }
return 0; } } Willy.
"Dave" <Da**@discussions.microsoft.com> wrote in message news:88**********************************@microsof t.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**@discussions.microsoft.com> wrote in message >> news:1F**********************************@microsof t.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...
|
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...
|
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...
|
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...
|
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...
|
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?
...
|
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:
...
|
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?
...
|
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
...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
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,...
|
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$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
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...
| |