|
I have an application in MFC which creates shared memory using CreateFileMapping where I share a Struct object. In another console application in C++, I am accessing the shared memory using OpenFileMapping function and so the struct object is changed/accessed in both the applications.
Now, I want to redevelop my MFC Application in WPF C#. Is it possible to share the memory with the same console application as I have done earlier in MFC.
Please advise
|
|
|
|
|
I know that the latest .Net frameworks offer classes for file mapping. Search "C# File mapping"
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Is it possible that the 2 applications that share the memory (File mapping) be developed in C# and C++ respectively.
Example:
One application in C# to create shared memory
Other application 2 in C++ opens and shares the memory file mapping.
|
|
|
|
|
Memory-mapped files have been supported in .NET Framework since 4.0:
Memory-Mapped Files | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I have created one C# dll of .Net Framework 4.7.2. In this DLL, I want to deserialize the JSON file and when I have added the code for deserialization ,I got compilation error to install the NuGet package
After installing System.Text.Json NuGet package of latest version(5.0.2), dll builds successfully.
When I connect this Dll with the C++ application and calls the deserialization method of the DLL, at that time I got one run time exception stating that:
“Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.”
Solutions I Tried:
1) I tried to change in the app.config file with the value of the version which is specified in the error.
After changing in app. Config file , I got different run time exception:
“System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.'”
I tried changing the version of System. Buffers also in config file, but still getting the same error.
2) I tried to install the System.Text.Json of version 4.7.2
In this version also I am getting the same System. Buffers error.
Note: If we use the .Net 5.0.0(Core version) then System.Text.Json already present in it as default but with this version we are not able the generate the .tlb file as Regasm.exe gives error while registering the dll and in .Net 5.0.0 Register for COM interop is also disabled.
Thank you in advance.
|
|
|
|
|
If your .DLL is written targeting .NET Framework 4.7.2, you can NOT use .NET Core 5.0 libraries.
Also, trying to just download those libraries and register them with RegAsm will NOT work.
What code did you write for handling the JSON files?
|
|
|
|
|
Dave Kreskowiak wrote: If your .DLL is written targeting .NET Framework 4.7.2, you can NOT use .NET Core 5.0 libraries.
System.Text.Json explicitly supports .NET Framework 4.6.1 or higher:
NuGet Gallery | System.Text.Json 5.0.2[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yeah, I forgot about .NET Standard Libraries.
|
|
|
|
|
The NuGet package you have referenced depends on other NuGet packages:
NuGet Gallery | System.Text.Json 5.0.2[^]
Specifically, for .NET Framework 4.x, it depends on:
- Microsoft.Bcl.AsyncInterfaces (>= 5.0.0)
- System.Buffers (>= 4.5.1)
- System.Memory (>= 4.5.4)
- System.Numerics.Vectors (>= 4.5.0)
- System.Runtime.CompilerServices.Unsafe (>= 5.0.0)
- System.Text.Encodings.Web (>= 5.0.1)
- System.Threading.Tasks.Extensions (>= 4.5.4)
- System.ValueTuple (>= 4.5.0)
You will need to make sure all of those packages, and all of their dependencies (and their dependencies' dependencies etc.), are present in the same folder as your dll.
Alternatively, change your code to use Json.NET , which has no dependencies:
NuGet Gallery | Newtonsoft.Json 13.0.1[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I was trying to login to a site programmatically. i am loading site into web browser control. i tried this code but it did not worked. https://auth.accounts.dowjones.com/login
Sorry could not share actual credentials.
HtmlElement username = null;
HtmlElement password = null;
HtmlElement submit = null;
foreach (HtmlElement divcontainer in webBrowser1.Document.GetElementsByTagName("div"))
{
if (divcontainer.GetAttribute("className") == "text-input")
{
foreach (HtmlElement child in divcontainer.Children)
{
if (divcontainer.GetAttribute("className") == "js-email-input")
{
username = divcontainer;
}
if (divcontainer.GetAttribute("className") == "password js-password-input")
{
password = divcontainer;
}
}
}
if (divcontainer.GetAttribute("className") == "sign-in")
{
foreach (HtmlElement child in divcontainer.Children)
{
if (divcontainer.GetAttribute("className") == "solid-button basic-login-submit")
{
submit = divcontainer;
}
}
}
}
if (username != null && password != null && submit != null)
{
username.SetAttribute("value", "test@gmail.com");
password.SetAttribute("value", "test11");
submit.InvokeMember("click");
}
please some one guide me what to change in my code to automate login.
Thanks
|
|
|
|
|
This seems logical. What's not working?
"Did not work" does not help. Did it click the button? Did the text appear in the controls? Did it start playing Pacman?
|
|
|
|
|
the code i have mention in my post which is not filling textbox and not clicking button. so help me where i made the mistake?
|
|
|
|
|
Either you didn't wait for the page to finish loading, or the elements in the page don't match the elements you're looking for.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
 Issue sorted. i have done the job this way and it worked.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && IsRedirect)
{
System.Threading.Thread.Sleep(5000);
timer1.Enabled = true;
}
else if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && IsloginPage)
{
mshtml.IHTMLDocument2 htmlDocument = (mshtml.IHTMLDocument2) webBrowser1.Document.DomDocument;
mshtml.IHTMLElementCollection objforms = htmlDocument.forms;
foreach (mshtml.HTMLFormElementClass form in objforms)
{
if (form.className == "form")
{
IHTMLElementCollection inputElements = form.getElementsByTagName("INPUT");
foreach (HTMLInputElementClass inputElement in inputElements)
{
if ((inputElement.type.Trim().ToLower() == "email") && (inputElement.name.Trim() == "email"))
{
inputElement.value = "Test_UID@gmail.com";
}
else if ((inputElement.type.Trim().ToLower() == "password") && (inputElement.name.Trim() == "password"))
{
inputElement.value = "Test_PWD";
}
}
IHTMLElementCollection inputElements1 = form.getElementsByTagName("BUTTON");
foreach (HTMLButtonElementClass inputElement in inputElements1)
{
if ((inputElement.innerText.ToLower() == "sign in") && (inputElement.type.Trim().ToLower() == "submit")
&& (inputElement.tagName.Trim().ToLower() == "button"))
{
inputElement.click();
break;
}
}
}
}
}
}
Thanks
|
|
|
|
|
code curve in c#.image processing
|
|
|
|
|
Was that supposed to be a question?
|
|
|
|
|
Do you mean the Graphics.DrawArc Method (System.Drawing) | Microsoft Docs[^]?
If not, you are going to have to explain in a lot more details as we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hey,
I'm new to C# and coding in general. I've done some reading and online "hands-on" tutorials but I'm truly stuck here. Can someone please help me out and review the code I have written? I'm willing to put the time in to learn, I just don't know where to go from here.
I'm having a problem with my switch statement that involves different levels of commission rates, as well as getting the calculation to work. Also, on my last line it is giving me an error -- saying that "dblCalculateCommission" is an unassigned variable. I haven't had this problem on similar exercises. Appreciate any help, ready to pull my hair out!
private void btnCalculateCommission_Click(object sender, EventArgs e)
{
// declare variables to be used in the calculation
double dblSalesPersonLevel;
double dblAmountSold;
double dblCalculateCommission;
// convert the values in the text boxes to numeric and place into variables
dblSalesPersonLevel = Convert.ToDouble(txtSalesPersonLevel.Text);
dblAmountSold = Convert.ToDouble(txtAmountSold.Text);
// check the sales level is between 1 and 4, if it is outside of these parameters display an error message
if (dblSalesPersonLevel < 1)
{
MessageBox.Show("Sales level cannot be less than 1", "Input Error");
txtSalesPersonLevel.SelectAll();
txtSalesPersonLevel.Select();
}
else if (dblSalesPersonLevel > 4)
{
MessageBox.Show("Sales level cannot be greater that 4", "Input Error");
txtSalesPersonLevel.SelectAll();
txtSalesPersonLevel.Select();
}
// Calculate commissions based on sales person level
{
string strSalesPersonLevel;
strSalesPersonLevel = txtSalesPersonLevel.Text;
switch (strSalesPersonLevel)
{
case "1":
{
dblCalculateCommission = 500 + (dblAmountSold * 0.02);
break;
}
case "2":
{
dblCalculateCommission = 750 + (dblAmountSold * 0.03);
break;
}
case "3":
{
dblCalculateCommission = 1000 + (dblAmountSold * 0.04);
break;
}
case "4":
{
dblCalculateCommission = 1250 + (dblAmountSold * 0.05);
break;
}
}
// Convert commission into a string and place into a label with currency
//formatting
lblCalculateCommission.Text = dblCalculateCommission.ToString("C2");
|
|
|
|
|
The code is incomplete and badly formatted so it's going to be a bit of guess work.
The first thing that sticks out to me is why is there a set of curly braces that don't belong?
{ <--- This shouldn't be here
string strSalesPersonLevel;
strSalesPersonLevel = txtSalesPersonLevel.Text;
...
case "4":
{
dblCalculateCommission = 1250 + (dblAmountSold * 0.05);
break;
}
} <--- This shouldn't be here
lblCalculateCommission.Text = dblCalculateCommission.ToString("C2");
|
|
|
|
|
Try something like this:
private void btnCalculateCommission_Click(object sender, EventArgs e)
{
if (!int.TryParse(txtSalesPersonLevel.Text, out var salesPersonLevel) || salesPersonLevel < 1 || salesPersonLevel > 4)
{
MessageBox.Show("Invalid sales person level.", "Input Error");
txtSalesPersonLevel.SelectAll();
txtSalesPersonLevel.Select();
return;
}
if (!double.TryParse(txtAmountSold.Text, out var amountSold))
{
MessageBox.Show("Invalid amount sold.", "Input Error");
txtAmountSold.SelectAll();
txtAmountSold.Select();
return;
}
double calculatedCommission;
switch (salesPersonLevel)
{
case 1:
{
calculatedCommission = 500 + (amountSold * 0.02);
break;
}
case 2:
{
calculatedCommission = 750 + (amountSold * 0.03);
break;
}
case 3:
{
calculatedCommission = 1000 + (amountSold * 0.04);
break;
}
case 4:
{
calculatedCommission = 1250 + (amountSold * 0.05);
break;
}
default:
{
calculatedCommission = 0;
break;
}
}
lblCalculateCommission.Text = calculatedCommission.ToString("C2");
} If you're using a recent compiler and C# 8 or later, you can use a switch expression instead:
double calculatedCommission = salesPersonLevel switch
{
1 => 500 + (amountSold * 0.02),
2 => 750 + (amountSold * 0.03),
3 => 1000 + (amountSold * 0.04),
4 => 1250 + (amountSold * 0.05),
_ => 0
}; switch expression - C# reference | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you both for the reply -- appreciate the help!
|
|
|
|
|
I have a problem with identifying printers,
the problem is how to unequivocally identify the printer to clearly state that, for example, after switching the printer to another computer, be sure that the printer has been switched and not the same second added.
It is possible to read the identifier from the PNPDeviceID field of the device (some of this is our identifier, I think so) listing the devices connected to the USB. One of them will be "Universal USB printer", i.e. our printer, but I have not found any information on how to pair the printer installed with the device connected to the USB.
Or some other better way to get S / N from a connected printer and assign it to a specific printer downloaded using e.g. System.Drawing.Printing.PrinterSettings.InstalledPrinters
Thx Piter
|
|
|
|
|
The only thing I found that came close to a S/N was the "PortName" (Brother printers in my case).
string query = "SELECT * FROM Win32_Printer";
ManagementObjectSearcher searcher = new ManagementObjectSearcher( query );
var services = searcher.Get();
foreach ( ManagementObject service in services ) {
var properties = service.Properties;
foreach ( PropertyData p in properties ) {
if ( p.Value == null ) { continue; }
Console.WriteLine( $"{p.Name}: [{p.Value}]" );
}
}
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hi,
SzakiII wrote: I have a problem with identifying printers First let me say that I am not a C# developer. All I can offer is the correct nomenclature and some guidance on how the operating system works and how I would obtain this using the native Windows API. So if you had 10 identical BrandX printers installed the Microsoft Windows operating system differentiates these by appending a unique device instance id[^] to the hardware id string.
As a native programmer I would call GetPrinterDriver function[^] with DRIVER_INFO_6[^] and obtain the hardware ID. Then I would tokenize the string and extract the unique device instance id[^]. You could also call the EnumPrinterDrivers function[^] to get DRIVER_INFO_6[^] for all of the installed printers. This would work for both local and network shared printers.
Maybe another codeproject member knows of a C# managed way to get to the device instance id. The unique identifier is at the end of the string:
PCI\VEN_1000&DEV_0001&SUBSYS_00000000&REV_02\1&08
Best Wishes,
-David Delaune
|
|
|
|
|
A "Hardware Id" (or "instance" id) is not same as a Serial Number.
Quote: A hardware ID is a vendor-defined identification string that Windows uses to match a device to an INF file. In most cases, a device has more than one hardware ID associated with it. Typically, a list of hardware IDs is sorted from most to least suitable for a device.
Hardware ID - Windows drivers | Microsoft Docs
If you look at the (contents of) "PortName", you will see the days of "LPT1, etc." are long gone (i.e. before serial numbers)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
modified 15-Jun-21 19:21pm.
|
|
|
|