|
I want to achieve in C#, same what this code achieves in C++. Is this possible? How? Preferable would be without using PInvoke
void CVersion::GetVersion(DWORD *v1, DWORD *v2, DWORD *v3, DWORD *Build) const
{
char Path[1024] ;
DWORD Len1, Len2 ;
VS_FIXEDFILEINFO *FileVerInf ;
if(GetModuleFileName(m_hModule, Path, 1024))
{
if((Len1 = GetFileVersionInfoSize(Path, &Len2)) != 0)
{
BYTE *InfoBlob = NULL ;
try
{
LPSTR lpstrVffInfo ;
UINT uVersionLen ;
InfoBlob = new BYTE[Len1+255] ;
if(!InfoBlob) throw -1 ;
lpstrVffInfo = (LPSTR)InfoBlob ;
GetFileVersionInfo(Path, Len2, Len1, lpstrVffInfo) ;
BOOL bRetCode = VerQueryValue((LPVOID)lpstrVffInfo,
(LPSTR)"\\",
(LPVOID *)&FileVerInf,
(UINT *)&uVersionLen);
if(!bRetCode)
{
FileVerInf->dwFileVersionLS = 0 ;
FileVerInf->dwFileVersionMS = 0 ;
}
if(v1 != NULL) *v1 = (FileVerInf->dwFileVersionMS&0xffff0000)>>16 ;
if(v2 != NULL) *v2 = FileVerInf->dwFileVersionMS&0x0000ffff ;
if(v3 != NULL) *v3 = (FileVerInf->dwFileVersionLS&0xffff0000)>>16 ;
if(Build != NULL) *Build = FileVerInf->dwFileVersionLS&0x0000ffff ;
} catch (...) {;}
if(InfoBlob != NULL) delete[] InfoBlob ;
}
}
}
If it is not possible without Pinvoke, can you show me some sample how to do this with PInvoke?
|
|
|
|
|
If you do not want to use PInvoke directly you could use a C++ DLL to return the information in a suitable format. See also P/Invoke tutorial[^].
|
|
|
|
|
Would you be kind to show me how to achieve similar with PInvoke? I am beginner with .NET
|
|
|
|
|
Go to the link I provided and follow the tutorial, although P/Invoke is somewhat advanced for a beginner. You can also find more useful information on The PInvoke website[^].
|
|
|
|
|
This is why I am saying it seems a bit advanced for me. This is how I have reached so far with bit of googling, maybe you can help further?
[System.Runtime.InteropServices.DllImport("coredll")]
private static extern bool GetFileVersionInfo(string filename, UInt32 handle, UInt32 len, IntPtr buffer);
[System.Runtime.InteropServices.DllImport("coredll")]
private static extern UInt32 GetFileVersionInfoSize(string filename, out UInt32 handle);
[System.Runtime.InteropServices.DllImport("coredll")]
private static extern bool VerQueryValue(IntPtr buffer, string subblock, out IntPtr blockbuffer, out int len);
UInt32 msb = (UInt32)System.Runtime.InteropServices.Marshal.ReadInt32(verbuffer, 8);
UInt32 lsb = (UInt32)System.Runtime.InteropServices.Marshal.ReadInt32(verbuffer, 12);
major = (msb & 0xFFFF0000) >> 16;
minor = (msb & 0x0000FFFF);
build = (lsb & 0xFFFF0000) >> 16;
revision = (lsb & 0x0000FFFF);
But I don't know what is verbuffer ?
|
|
|
|
|
You need to allocate the version info structure as described on MSDN. You then need to make the calls to the various routines (not just declare them) via the pinvoke calls in order to get the structure filled in with the information.
|
|
|
|
|
what about my other approach - see comment below. It showed similar version with one DLL when I tested
|
|
|
|
|
user20044 wrote: what about my other approach What about it? You need to test your code and check whether it returns the information you want. If it does not get the correct results, then provide the full details so people can try to help.
|
|
|
|
|
This is my newer approach. Do you think this is fine?
var versInfo = FileVersionInfo.GetVersionInfo(dllPath);
return String.Format("{0}.{1}.{2}.{3}", versInfo.FileMajorPart, versInfo.FileMinorPart, versInfo.FileBuildPart, versInfo.FilePrivatePart);
|
|
|
|
|
Are you trying to get the assembly version?
If so, then it's trivial in C#:
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
My code will run inside DLL, and this needs to get probably(I am not sure) version of DLL
|
|
|
|
|
Try it: That's what GetExecutingAssembly does - returns the DLL if the code is in a DLL, and the EXE if it's still in the original app assembly.
There is another methods to get the EXE directly: Assembly.GetEntryAssembly[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
You can see my approach in response above to Richard, any comments?
var versInfo = FileVersionInfo.GetVersionInfo(dllPath);
return String.Format("{0}.{1}.{2}.{3}", versInfo.FileMajorPart, versInfo.FileMinorPart, versInfo.FileBuildPart, versInfo.FilePrivatePart);
modified 29-Oct-15 9:06am.
|
|
|
|
|
There is File version info and Assembly version info. The two are not the same.
The question is which one are you really looking for.
|
|
|
|
|
The one I am looking for is which is used in C++ code I showed
|
|
|
|
|
I have one app which freezes UI on StartWipeFile() function call..How to solve problem????plz help
try
{
var result = MsgBox.MsgGridVisbility("Alert",
"Do you want to continue for disk wipe ?", 2,
Config.MsgBoxFontColor, Config.MsgBoxHeaderBgColor, Config.MsgBoxHighlightedBtnColor,
Config.MsgBoxNormalBtnColor);
if (!result.Equals("2"))
{
BusyBar.IsBusy = false;
return;
}
this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
{
BusyBar.IsBusy = true;
StartWipeFile();
});
}
finally
{
BusyBar.IsBusy = false;
}
}
private void StartWipeFile()
{
try
{
CreateFile(drivelist.SelectedValue.ToString());
wipe.WipeFile(fileName, Convert.ToInt32(Regex.Match(passlist.SelectedValue.ToString(), @"\d+").Value));
}
catch (Exception)
{
BusyBar.IsBusy = false;
}
finally
{
BusyBar.IsBusy = false;
}
}
public void CreateFile(string DriveNameSelectedValue)
{
try
{
char p = Convert.ToChar(DriveNameSelectedValue.Remove(1, 2).Cast<char>());
DriveManager.FormatDrive(p, "New Folder");
var disksize =
Convert.ToDouble(String.Format("{0 .00}",
AvailableFreeSpaceFormatted(DriveNameSelectedValue, DiskSizeUnit.Bytes)));
//var data = ConvertGigabytesTobytes(Convert.ToDouble(memorytxt.Text));
//var data = ConvertGigabytesTobytes(Convert.ToDouble(AvailableFreeSpaceFormatted(drivelist.SelectedItem.ToString(), DiskSizeUnit.GigaBytes)));
//string fileName = @"D:\\Text.txt";
fileName = @"" + DriveNameSelectedValue + "WXF1ENCR.txt";
if (File.Exists(fileName))
{
File.Delete(fileName);
}
var fs = new FileStream(fileName, FileMode.CreateNew);
// Create the writer for data.
var w = new BinaryWriter(fs);
// Write data to Test.data.
try
{
for (int i = 0; i < disksize; i++)
{
w.Write((byte) i);
}
}
catch (IOException)
{
//w.Close();
//fs.Close();
//return fileName;
//return Convert.ToString(0);
}
//w.Flush();
w.Close();
//fs.Flush();
fs.Close();
//return fileName;
}
catch (Exception ex)
{
MessageBox.Show((ex.Message));
}
}
|
|
|
|
|
If it's freezing the UI, then it's probably because the file wipe is taking a considerable amount of time - and because it's all being done in the same thread, the UI can't update until the long running task is complete.
Move your Wipe into a different thread and it should "unfreeze" the UI. Have a look at the BackgroundWorker class[^] - it's easy to use and provides event for progress reportign and task completion.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
1. You need to call StartWipeFile() on a different thread.
2. When posting, check that you don't mis-paste the code - such as showing & quot; etc.
3. Do a little debugging yourself - just clicking the "Break into debug" would have shown you the problem
Best,
John
|
|
|
|
|
Hello All exoperts,
below code which I saw in code project wi;l;l do a pivot of a data set where x,y,z axis are fixed, I just want to have 4 column to be used as y axis , can I do that ?
what I understood frpom the code,
y axis value is stored in a ;list
List<string> columnYValues = new List<string>();
and it was return at
foreach (string columnYValue in columnYValues)
{
//Creates a new Row
DataRow drReturn = returnTable.NewRow();
drReturn[0] = columnYValue;
can I return other y axis cpo;lumn in same way with in the same ;loop\.
///
/// Gets a Inverted DataTable
///
/// <param name="table" />Provided DataTable
/// <param name="columnX" />X Axis Column
/// <param name="columnY" />Y Axis Column
/// <param name="columnZ" />Z Axis Column (values)
/// <param name="columnsToIgnore" />Whether to ignore some column, it must be
/// provided here
public static DataTable GetInversedDataTable(DataTable table, string columnX,
string columnY, string columnZ, string nullValue, bool sumValues)
{
//Create a DataTable to Return
DataTable returnTable = new DataTable();
if (columnX == "")
columnX = table.Columns[0].ColumnName;
//Add a Column at the beginning of the table
returnTable.Columns.Add(columnY);
//Read all DISTINCT values from columnX Column in the provided DataTale
List<string> columnXValues = new List<string>();
foreach (DataRow dr in table.Rows)
{
string columnXTemp = dr[columnX].ToString();
if (!columnXValues.Contains(columnXTemp))
{
//Read each row value, if it's different from others provided, add to
//the list of values and creates a new Column with its value.
columnXValues.Add(columnXTemp);
returnTable.Columns.Add(columnXTemp);
}
}
//Verify if Y and Z Axis columns re provided
if (columnY != "" && columnZ != "")
{
//Read DISTINCT Values for Y Axis Column
List<string> columnYValues = new List<string>();
foreach (DataRow dr in table.Rows)
{
if (!columnYValues.Contains(dr[columnY].ToString()))
columnYValues.Add(dr[columnY].ToString());
}
//Loop all Column Y Distinct Value
foreach (string columnYValue in columnYValues)
{
//Creates a new Row
DataRow drReturn = returnTable.NewRow();
drReturn[0] = columnYValue;
//foreach column Y value, The rows are selected distincted
DataRow[] rows = table.Select(columnY + "='" + columnYValue + "'");
//Read each row to fill the DataTable
foreach (DataRow dr in rows)
{
string rowColumnTitle = dr[columnX].ToString();
//Read each column to fill the DataTable
foreach (DataColumn dc in returnTable.Columns)
{
if (dc.ColumnName == rowColumnTitle)
{
//If Sum of Values is True it try to perform a Sum
//If sum is not possible due to value types, the value
// displayed is the last one read
if (sumValues)
{
try
{
drReturn[rowColumnTitle] =
Convert.ToDecimal(drReturn[rowColumnTitle]) +
Convert.ToDecimal(dr[columnZ]);
}
catch
{
drReturn[rowColumnTitle] = dr[columnZ];
}
}
else
{
drReturn[rowColumnTitle] = dr[columnZ];
}
}
}
}
returnTable.Rows.Add(drReturn);
}
}
else
{
throw new Exception("The columns to perform inversion are not provided");
}
//if a nullValue is provided, fill the datable with it
if (nullValue != "")
{
foreach (DataRow dr in returnTable.Rows)
{
foreach (DataColumn dc in returnTable.Columns)
{
if (dr[dc.ColumnName].ToString() == "")
dr[dc.ColumnName] = nullValue;
}
}
}
return returnTable;
}
|
|
|
|
|
If you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Member 12061647 wrote: below code which I saw in code project Please post your question in the forum at the end of the article so the author can help you.
|
|
|
|
|
I have a Hm-tr module and i want to observe it output in a box
I connect it to my pc and i want observe the data that has been received in my pc. it is important that the program writen with c# but if u have any other idea,tell me.
tnk a lot.
|
|
|
|
|
|
|
Context.InvokeAsynchronousTask(() =>
{
});
I have this code, its not getting called from the unit test project.
|
|
|
|