Chris 的个人资料Cappy's Blog - Live Spac...照片日志列表 工具 帮助

日志


2月20日

Why use (local) or “.” as a SQL Server Name?

You learn something new every day I suppose and I was taught something today that was complete news to me. Keep in mind I'm not exactly a database expert, so for those DBAs out there you can go ahead a stop reading now. I've used (local) or a period as the server name in connection strings and thought it was just a simple way of forcing usage of the local machine name. I guess that's true in part, but there's more. Using either of these tells SQL to use Shared Memory and does not require that either Named Pipes or TCP/IP protocols to be enabled. It's probably not much use in a production environment, but great for developing on local SQL instances.

Let me know if you find this useful, as I'd like to know if I'm the only SQL dummy out there.

Chris

12月18日

Pre-allocated Strings in Win32 API

As a part time developer, I can say that I have nowhere near mastered C#. When I find something that took me quite a while to figure out I might as well post it here in hopes it will make someone else's life easier. My problem was calling a Win32 API from within C# (PInvoke) with a char* that needed to be pre-allocated. Example:

long JustATest (long handle, char * pPreallocatedText)

Normally in the scenario of char* I would simply pass a type of string. This works fine until the API needs the string to already be allocated. From my test, there was no way to get the call to succeed using:

 

[DllImport("sample.dll", EntryPoint = "JustATest")]

public static extern int JustATest(int handle, string text);

What I finally discovered (thanks to another blog) was that you can use a StringBuilder instead and just pre-allocate its size:

[DllImport("sample.dll", EntryPoint = "JustATest")]

public static extern int JustATest(int handle, StringBuilder text);

To make the call simply create a StringBuilder:

StringBuilder text = new StringBuilder(2000);

Than call the API.

int iResult = JustAtest(myHandle, text);

This seemed to work perfectly. Hope this helps someone.

Chris