background preloader

.NET

Facebook Twitter

Marshaling Data with Platform Invoke. .NET Framework 4.6 and 4.5 To call functions exported from an unmanaged library, a .NET Framework application requires a function prototype in managed code that represents the unmanaged function. To create a prototype that enables platform invoke to marshal data correctly, you must do the following: The following table lists data types used in the Win32 API (listed in Wtypes.h) and C-style functions. Many unmanaged libraries contain functions that pass these data types as parameters and return values. The following code defines the library functions provided by Pinvoke.dll.

Secure Salted Password Hashing - How to do it Properly. If you're a web developer, you've probably had to make a user account system. The most important aspect of a user account system is how user passwords are protected. User account databases are hacked frequently, so you absolutely must do something to protect your users' passwords if your website is ever breached. The best way to protect passwords is to employ salted password hashing. This page will explain why it's done the way it is. There are a lot of conflicting ideas and misconceptions on how to do password hashing properly, probably due to the abundance of misinformation on the web. IMPORTANT WARNING: If you are thinking of writing your own password hashing code, please don't!. If for some reason you missed that big red warning note, please go read it now.

You may use the following links to jump to the different sections of this page. There is BSD-licensed password hashing source code at the bottom of this page: What is password hashing? Hash algorithms are one way functions. Using the Reliability Features of the .NET Framework. ExecuteCodeWithGuaranteedCleanup erects a structured exception handling (SEH) handler within the CLR. With that protection in place, the method calls back into the managed TryCode. If a stack overflow occurs within that code, the runtime catches it and ensures that enough stack space exists to run CleanupCode correctly.

Of course, you still need to make sure that the back-out code itself does not cause a stack overflow (it should not make highly recursive calls or depend on code that requires a large or unknown amount of stack space). As an aside, note that C# doesn't currently allow you to specify a custom attribute on an anonymous method, so try not to use anonymous methods for your back-out code. CLR Hosts and Escalation Policies When does an out-of-memory condition not result in an OutOfMemoryException? When a CLR host says it shouldn't.

These types of policies aren't enough for all hosts. Critical Finalizers and Safe Handles. Using Connection String Keywords with SQL Server Native Client. There are two ways OLE DB applications can initialize data source objects: IDBInitialize::Initialize IDataInitialize::GetDataSource In the first case, a provider string can be used to initialize connection properties by setting the property DBPROP_INIT_PROVIDERSTRING in the DBPROPSET_DBINIT property set. In the second case, an initialization string can be passed to IDataInitialize::GetDataSource method to initialize connection properties. Both methods initialize the same OLE DB connection properties, but different sets of keywords are used. The set of keywords used by IDataInitialize::GetDataSource is at minimum the description of properties within the initialization property group. Any provider string setting that has a corresponding OLE DB property set to some default value or explicitly set to a value, the OLE DB property value will override the setting in the provider string. connection-string ::= empty-string[;] | attribute[;] | attribute; connection-string empty-string ::=

.NET Delegates: A C# Bedtime Story. The following is an excerpt from Windows Forms 2.0 Programming, Chris Sells & Michael Weinhardt, Addison-Wesley, 2006. It's been updated from the original version for C# 2.0. Once upon a time, in a strange land south of here, there was a worker named Peter. He was a diligent worker who would readily accept requests from his boss. However, his boss was a mean, untrusting man who insisted on steady progress reports. Since Peter did not want his boss standing in his office looking over his shoulder, Peter promised to notify his boss whenever his work progressed. Peter implemented this promise by periodically calling his boss back via a typed reference like so: Interfaces Now Peter was a special person.

Delegates Unfortunately, Peter was so busy talking his boss into implementing this interface that he didn't get around to notifying the Universe, but he knew he would soon. Still, his boss complained bitterly. Static Listeners Events Peter wants to make sure that neither of these can happens. How to create a COM object using VS 2008 and consume it from VB 6.0 client application: Part II.

In order to perform that, I invite you to follow this walkthrough. Walkthrough Use case: Given that serialization process is not possible under Visual Basic 6.0, you will create a COM object that could be consumed locally by a Visual Basic 6.0 client application by exposing an initial .Net assembly to COM+. To follow this walkthrough, Visual Studio 2008 and Visual Basic 6.0 have to be installed within your machine and/or Microsoft Office 2003/2007.

A. Build the COM serviced component Remarque2: In this case, I used regsvcs.exe as a tool to expose the .Net assembly to COM but you still can use tlbexp.exe to export the assembly to the *.tlb form and regsvr32.exe to register the product within the registry entry. Before exposing the method of how to consume this COM object let's take a look at the bin directory of your project Figure 6 As you remark, there is the nostalgic *.tlb file in addition to the COM.dll. Figure 7 You will remark the new registered COM serviced component. Figure 8 Figure 9. Download LINQPad. Using .NET: Avoid Common Performance Pitfalls for Speedier Apps. Reflection Dodge Common Performance Pitfalls to Craft Speedy Applications Joel Pobar Parts of this article are based on a prerelease version of the .NET Framework 2.0.

Those sections are subject to change. This article discusses: How to get good reflection performance Early-bound and late-bound invocation Member caching and handles Best practices for using reflection This article uses the following technologies: .NET Framework, C# Using reflection efficiently is like haggling with an API. In this article I'll show you which reflection tasks are costly and which ones are not. What's Slow and What's Not? Outlines some commonly used reflection APIs. Fast and Light Functions typeof Object.GetType typeof == Object.GetType Type equivalence APIs (including typehandle operator overloads) get_Module get_MemberType Some of the IsXX predicate APIs New token/handle resolution APIs in the .NET Framework 2.0 Costly Functions When Should You Use Reflection?

Obviously this is not optimal. Invoking or Calling a Member. Using the DropDownList and ListBox with ASP.NET MVC in C# for Visual Studio 2010.