marți, 4 februarie 2020
Execute a Python script using C#
One of the best posts on the topic can be found on CodeProject:
https://www.codeproject.com/Articles/5165602/Using-Python-Scripts-from-a-Csharp-Client-Includin
luni, 15 mai 2017
WPF Recipes - Binding of the RichTextBox Text
<RichTextBox>
<FlowDocument>
<Paragraph>
<Run Text="{Binding MyText}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
vineri, 4 decembrie 2015
Windows Search Indexer High CPU
The CPU was at 80-90%. I looked up for a solution and found it here:
http://answers.microsoft.com/en-us/windows/forum/windows_8-performance/microsoft-windows-search-filter-host-is-burning-my/bae4c974-5b47-4ab2-b17d-c599853f00f1?auth=1
It seems to be an older problem, prior to Windows 10.
To solve this I went to Control Panel and opened "Indexing Options". On the top of the window, I could see that the index was being built. I then went to "Advanced" and under the "Index Settings" tab, in the "Troubleshooting" section, I chose "Rebuild". This deletes the index and rebuilds it. Apparently there is "an error with the index" that causes the CPU consumption. As soon as I chose to rebuild, the CPU went down to reasonable values, below 5%.
These are the processes to look for in Task Manager:
vineri, 22 mai 2015
marți, 6 ianuarie 2015
Why Declare a Function Static in C?
Making functions static only makes them visible to the translation unit (.c file) that you declare them into and may even bring performance improvements when compiler optimization is enabled.
Full lenght discussion on Stack Overflow:
http://stackoverflow.com/questions/5319361/static-function-in-c
vineri, 29 august 2014
Productivity in the Open
This is very accurate in my opinion.
I recently ran into this post on The Economist and found it quite compelling in the sense that we need to rather focus than waste a lot of time fidgeting or wondering about the office with no clear purpose at hand. Fewer working hours, more personal life, more creativity and improvement you can bring to the world.
http://www.economist.com/blogs/freeexchange/2014/12/working-hours
duminică, 29 septembrie 2013
Core C++ Series
Unable to Access the Properties Window in Visual Studio
This had to be something about settings, because when I was running a debugging session, the Properties window would be displayed.
I had two hypotheses, with the help of Spy++. I could see the window listed under Visual Studio 2005 in Spy++ so it had to be somewhere.
Inspecting the properties of the Visual Studio 2005 'Properties' window, I got:
So:
1. The window is either off screen and/or
2. The window is of zero width as shown in the rectangle properties.
I tried locating registry settings to specify the location and/ or dimension of the Properties window, but without success.
My solution was to restore Visual Studio's settings for C# development environment (which I spend most of my time in) and this restored the views of all windows to default and now I can access the Properties window as before. ( see this Microsoft How To for details on how to restore your settings)
The new Window Properties in Spy++ now look like this:
After I wrote this post I found this blog entry suggesting a solution for the situation when the 'Properties' window is off screen.
sâmbătă, 31 august 2013
How to Check for Windows CE Platform Builder Updates
C:\Program Files\Microsoft Platform Builder\6.00\cepb\SustainedEngineering
Running this tool and selecting the "Verify Updates" button will list the updates that you have installed and the ones that you have not yet installed, categorized by year. This will allow you to locate and install the latest update pack much easier on the Microsoft website.
The latest release is R3, and after that there is a cumulative update and for 2013 there are monthly updates that need to be installed one at a time.
miercuri, 14 august 2013
Passing structures to C++ using C#
I have searched the web and I have found a few threads looking into this problem as well as the official MSDN documentation.
http://stackoverflow.com/questions/3939867/passing-a-structure-to-c-api-using-marshal-structuretoptr-in-c-sharp
http://objectmix.com/dotnet/796796-ptrtostructure-failing-structure-must-not-value-class.html
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.structuretoptr.aspx
For my own needs, I am using the following code snippet, that is used to pass a structure by reference to a C++ function and get back the structure filled with the data.
In the code below, the StructureToPass structure must match the definition of the equivalent C++ structure and must be decorated with the StructLayout(LayoutKind.Sequential) attribute.
[StructLayout(LayoutKind.Sequential)]
public struct StructureToPass
{
public uint x;
public float y;
public float z;
}
StructureToPass tsData = new StructureToPass();
IntPtr strPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tsData));
try
{
Marshal.StructureToPtr(tsData, strPtr, false);
if ( 0 == GetStructureFromCPP(strPtr))
{
tsData = (StructureToPass)Marshal.PtrToStructure(strPtr, typeof(StructureToPass));
}
}
}
catch (Exception ex)
{
}
finally
{
Marshal.FreeHGlobal(strPtr);
}
Visual Studio 2005 C1001 compiler error
Cleaning and rebuilding the project helped me solve the issue.
More on internal compiler errors can be found here:
http://support.microsoft.com/kb/305980
though it was not the case for my particular situation.
marți, 11 septembrie 2012
Fast Facts - BinaryReader
This post is about the BinaryReader class and the ReadBytes method return value.
public virtual byte[] ReadBytes( int count )
This method returns an array of count bytes or the number of bytes left to the end of the underlying stream, whichever is less.
When there are no more bytes to read, calling ReadBytes return an empty array of bytes, not a null value. This is useful to know when verifying the outcome of a read operation.
To prove this, there is a quick C# method that you can load in LINQPad:
public static void Main() { const int arrayLength = 20; // Create random data to write to the stream. byte[] dataArray = new byte[arrayLength]; new Random().NextBytes(dataArray); BinaryWriter binWriter = new BinaryWriter(new MemoryStream()); // Write the data to the stream. Console.WriteLine("Writing the data."); binWriter.Write(dataArray); // Create the reader using the stream from the writer. BinaryReader binReader = new BinaryReader(binWriter.BaseStream); // Set Position to the beginning of the stream. binReader.BaseStream.Position = 0; // Read and verify the data. byte[] verifyArray = null; for( int i = 0; i < 6; i++ ) { // ReadBytes returns an empty array when there are no more bytes to read. verifyArray = binReader.ReadBytes(sizeof(uint)); Console.WriteLine( (verifyArray!=null) ? "Length = " + verifyArray.Length : "Array is null" ); verifyArray = null; } }
Running this program will display:
Writing the data.
Length = 4
Length = 4
Length = 4
Length = 4
Length = 4
Length = 0
vineri, 31 august 2012
DllImport default path
When using DllImport for interop in C#, the default path of the library that you want to import is dictated by the operating system. The search order used by Windows OS depends on whether SafeDllSearchMode is enabled or disabled. When the safe mode is enabled, the current directory is placed lower in the search order. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx for complete information on Dinamyc-Link Library Search Order.
Beside the implicit search order described below, one can use the .dll full path with the DllImport attribute.
The search order when safe mode is enabled is:
1.The directory from which the application loaded.
2.The system directory. Use the GetSystemDirectory function to get the path of this directory.
3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
4.The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
5.The current directory.
6.The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
And when the safe mode is disabled:
1.The directory from which the application loaded.
2.The current directory.
3.The system directory. Use the GetSystemDirectory function to get the path of this directory.
4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
5.The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6.The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
I have successfully used the directory from which the application is loaded, but it will be interesting to give a try to the other modes. Probably the most efficient is the use the first option most of the time for better performance, just because it's the first place the OS looks for the .dll. Knowing where the OS will look for the .dll is essential and a point to remember.
duminică, 19 februarie 2012
TIOBE index
TIOBE Software measures programming languages indexes with respect to their popularity among developers. The latest rankings are available here.
This month Java is the leader and has been maintaining its position since February last year, closely followed by C, which also maintains its position. The next three positions are held by C#, C++ and Objective-C. This is an interesting ranking and specially, it is good to know where the demand is going when starting to learn a new programming language. Compared to last year, C#, which I favor over Java simply because I spend most of my time developing for the Windows platform, jumped three positions and it now holds 3rd place, even though at significant distance from positions 1 and 2 (about 8 percent). I do not expect C# to be #1 any time soon, specially since it is not (natively) portable to Unix-like systems, and of course C is a natural leader, even though complex, no serious developer should lack knowledge of C.
Regarding the leader, Java, I remember I liked it a low while in college and after that and I still develop some pieces of code using it and I think it is all right, but when it comes to Windows desktop programming and developing software that has a user interface, I don't see it as a rival for C# right now. Well, this is a kind of debate that would not end if put on a forum or discussion group and of course, everyone would be right, since the best programming language is the one that you have used the most and feel most confortable using, no doubt about it. Enjoy coding!
marți, 14 februarie 2012
Add Command Prompt Shortcut to Windows Explorer Context Menu
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\Command Prompt] @="Command Prompt" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\Command Prompt\command] @="cmd.exe /k pushd %1"Otherwise, open the registry editor and browse to [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell]. Expand this key and create a new key named Command Prompt. Under the newly created Command Prompt create a new key and name it command. Double click the Default entry under command and assign it the value:
cmd.exe /k pushd %1And that will do the trick. Hopefully, next time I'll remember...
joi, 19 ianuarie 2012
Could not load file or assembly or one of its dependencies.
The full error text is in fact:
Could not load file or assembly 'xxx.dll' or one of its dependencies. An attempt was made to load a program with an incorrect formatand may occur when using Visual Studio 2005 on 64-bit platforms. A common cause of this error is when using third party tools that require the license compiler (lc.exe). On 64 bit systems, the license compiler will run as a 64-bit application by default and if the third party libraries are not compatible with 64-bit platforms, the aforementioned error is generated. One solution to fix this, even though in this case you will be limited to compiling your whole application in x86 mode, is to add the following code in the pre-build and post-build event sections of the project's Build Events section. The statements must be on a single line.
Pre-Build:
if exist %WINDIR%\Microsoft.NET\Framework64\v2.0.50727\ %WINDIR%\Microsoft.NET\Framework64\v2.0.50727\ldr64.exe setwowPost-Build:
if exist %WINDIR%\Microsoft.NET\Framework64\v2.0.50727\ %WINDIR%\Microsoft.NET\Framework64\v2.0.50727\ldr64.exe set64For more information on the license compiler, see this post on MSDN: http://msdn.microsoft.com/en-us/library/ha0k3c9f.aspx. I must say I have tried other solutions, as explained in other blogs or forums on-line, which advise to change the target platform to x86 or Any CPU, but those did not work for me.
marți, 3 ianuarie 2012
Show Control in Visual Studio 2005 Toolbox
luni, 2 ianuarie 2012
The path is not of a legal form.
I post the error stack below, for reference:
The path is not of a legal form.
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.Path.NormalizePath(String path, Boolean fullCheck)
at System.IO.Path.GetFullPathInternal(String path)
at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
Microsoft.VisualStudio.Design.VSTypeResolutionService.AddProjectDependencies(Project project)
Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.get_Assembly()
Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.Search(String fullName, String typeName, Boolean ignoreTypeCase, Assembly& assembly, String description) at
Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchProjectEntries(AssemblyName assemblyName, String typeName, Boolean ignoreTypeCase, Assembly& assembly) at
Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchEntries(AssemblyName assemblyName, String typeName, Boolean ignoreCase, Assembly& assembly, ReferenceType refType) at
Microsoft.VisualStudio.Design.VSTypeResolutionService.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, ReferenceType refType) at
Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError, Boolean ignoreCase) at
Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError) at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.GetType(ITypeResolutionService trs, String name, Dictionary`2 names) at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.FillStatementTable(IDesignerSerializationManager manager, IDictionary table, Dictionary`2 names, CodeStatementCollection statements, String className) at
System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration) at
System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager) at
Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) at
Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)
marți, 13 decembrie 2011
The refactoring could not be performed due to build errors
Use command line option '/keyfile' or appropriate project settings instead of 'AssemblyKeyFile'and it was not until I solved this error by commenting the AssemblyKeyFile line in the AssemblyInfo.cs file that the refactoring ( encapsulating a field ) started to work once again. You can see more details about this warning here:
http://msdn.microsoft.com/en-us/library/xh3fc3x0(v=VS.80).aspx.
The error is confusing since it says that one of the projects in the solution does not currently build, which is false since this is solely a warning.
duminică, 6 noiembrie 2011
Can you see with your tongue?
Seeing with your tongue may indeed happen to a certain level because of a property of the brain called "plasticity" which is in fact the ability of a brain region to interpret sensors from different sources and adapting itself to them such that for instance it is possible to "see" using our auditory cortex.
This plasticity of the brain may allow humans to enhance their senses in a way that may be a little scary to imagine right now and would be more or less associated to science-fiction. However, this is a road in defining what and who we are and understanding how the sensory, cognitive and emotional interleave.
The more answers we have, the more of this behavior we can replicate in machines. This is a long and open matter of discussion, which I think will represent the challenge of this century. Having thinking machines will mean a great shift in how we see society in the present days and it's hard to grasp at this point.
Meanwhile, noteworthy discoveries in how the brain works are the foundation bricks to an uncertain building.
So, can you see with your tongue?


