Damir Arh's Corner
Search
Categories
  Development
 .NET
 Batch
 C++
 SQL
 VB6
 Vista
 Web
 Win32
  Downloads
 Amiga
 Articles
 Presentations
 Sources
 Windows
  Personal
 Education
 Software
 Website
Archives
July, 2008 (1)
June, 2008 (1)
April, 2008 (2)
December, 2007 (1)
November, 2007 (3)
July, 2007 (4)
June, 2007 (1)
May, 2007 (2)
March, 2007 (3)
January, 2007 (1)
December, 2006 (4)
October, 2006 (5)
September, 2006 (3)
August, 2006 (2)
June, 2006 (8)
May, 2006 (5)
April, 2006 (1)
March, 2006 (4)
February, 2006 (3)
January, 2006 (3)
March, 2003 (1)
February, 2002 (1)
January, 2002 (2)
August, 2001 (1)
July, 2001 (1)
February, 2001 (1)
December, 2000 (1)
September, 2000 (1)
July, 2000 (1)
Other Sites
Potepanja v naravi (sl)
Picasa Web Albums (sl)
moj-album.com Gallery (sl)
Bolha.com Auctions (sl)
My Game Space
LinkedIn Public Profile
My GamerTag
Sponsored Links
Administration
Sign In
Sunday, May 28, 2006

Windows 2003 anonymous access to shared folders (Personal | Software)

Due to tightened default security in Windows 2003 the file shares cannot be accessed without logon in a domainless environment even if both shares and folders are set up to allow access to Everyone.

A bit of googling returns many different suggestions for solving the problem, none of which really seems to work for sure. I’m just adding my two cents to this confusion, hoping that this posting will help me the next I’ll be solving the same problem.

It’s all about configuring the Security options in Local Security policy. The logical path of enabling the Network Access: Let Everyone permissions apply to anonymous users policy and disabling the Network access: Restrict anonymous access to Named Pipes and Shares policy didn’t help. On the other hand enabling the Accounts: Guest account status policy and setting the Network access: Sharing and security model for local accounts policy to Guest only - local users authenticate as Guest did the trick. Don't forget to call gpupdate after changing the policy to enforce it immediately.

I’m not asserting that this is THE solution but it worked for me. However, you should be aware of the implications of enabling the guest account before doing it to solve your immediate problem.

A few additional words on the last mentioned policy change: It proves useful when the file server and the client have the same usernames defined but the passwords don’t match because it forces the client to login as guest. By default the client tries to login to the server with wrong password which once again causes the login prompt to appear. It is useful to keep the default setting when the password is the same because the auto login allows for granularity in security settings if more than equal permissions for everyone are needed.

5/28/2006 2:49:50 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Marshalling System.String to char* and vice-versa (Development | .NET | C++)

By switching from C# with P/Invoke calls to Managed C++ when implementing a managed wrapper for the ANSI C style library I stumbled upon, I wanted to avoid the tedious and error-prone task of writing the P/Invoke signatures for function calls and user-defined types for the structures they used. As a side result I also managed to avoid most of the advanced marshalling issues with complex data structures.

With simple value types not needing any explicit marshalling only strings need special attention since char* and System::String can’t be implicitly converted between. The Marshal class implements the methods necessary for doing this as demonstrated in the following snippet:

String^ MCPP::Together(String^ first, String^ second)
{
   // marshal managed strings to unamanged memory
   IntPtr firstPtr = Marshal::StringToHGlobalAnsi(first);
   IntPtr secondPtr = Marshal::StringToHGlobalAnsi(second);

   // cast unmanaged buffer to a characer array
   char* firstNative = static_cast<char*>(firstPtr.ToPointer());
   char* secondNative = static_cast<char*>(secondPtr.ToPointer());

   // perform some unmanaged calls
   int bufferSize = strlen(firstNative) + strlen(secondNative) + 4;
   char* resultBuffer = new char[bufferSize];
   sprintf_s(resultBuffer, bufferSize, "%s + %s", firstNative, secondNative);
  
   // marshal unmanaged character array to managed string
   String^ result = Marshal::PtrToStringAnsi(static_cast<IntPtr>(resultBuffer));
  
   // free all unmanaged buffers
   delete[] resultBuffer;
   Marshal::FreeHGlobal(firstPtr);
   Marshal::FreeHGlobal(secondPtr);
  
   // return managed string
   return result;
}

It should be noted that it wouldn’t make any sense switching to unmanaged code to do some string operations only as shown above. This is for demonstration purposes only and you would usually call some unmanaged libraries or the like instead of it. But it is a nice demonstration how cumbersome string operations were in C. If you were doing this once, the sprintf_s function call might have caught your attention. It’s a safe implementation of sprintf which prevents buffer overflows.

Another point worth mentioning is that you have to take care of allocated memory for the conversion since your character array is now allocated on the unmanaged heap. Make sure you use the FreeHGlobal method to do it, not the free (used for freeing memory allocated by *alloc calls – ANSI C style) or delete (used for freeing memory allocated by new calls – C++ style) functions.

To reduce the code overhead of string conversions between managed and unmanaged world you might consider wrapping it into a helper class. This should also help preventing unwanted memory leaks during the conversions.

5/28/2006 1:41:03 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [2]

Sunday, May 14, 2006

Implementing a managed interface in C++ (Development | .NET | C++)

Less than a month ago I was certain I’d never have to write managed code in C++. With such a set of mind I didn’t really care about the new features being added and improvements being made to managed C++ since the first Visual Studio .NET. Luckily there were people thinking otherwise and as a result this – often overlooked – member of the .NET family got better with every release and is actually quite nice in the 2005 version. Don’t get me wrong – it still is C++ and you probably wouldn’t want to use it if you could get away with C# or VB. But if platform invoke is giving you too many headaches you might want to take a look at it. It could be the easiest way to solve your problem.

To make this a little bit easier for those of you who haven’t used C++ for some time and didn’t bother to check managed C++ at all, I’ll provide a small sample to help out with the beginner’s problems I had when starting out.

In a situation like mine you’ll usually want to write a class library to wrap the unmanaged calls for undisturbed use within you managed project. In my case I’ll be implementing an interface but writing a standalone class is not much different.

Let’s start with the header file. You might remember that C++ requires the separation of class declaration from method definition, the former being done in a header (*.h) file and the latter in a code (*.cpp) file. I admit being a little bit spoiled by not having to do that any more in C#. Let’s name our header MCPP.h (fell free to guess what it stands for):

using namespace System;

namespace DamirsCorner
{
   namespace Samples
   {
      namespace LateBinding
      {
         public ref class MCPP : IBind
         {
         public:
            virtual String^ Together(String^ first, String^ second);
         };
      }
   }
}

It looks like a hybrid between C++ and C# and one could argue that’s what it actually is. I’d like to point out the following:

  • The using directive has an additional keyword namespace. Also a double colon (static separator in C++) would be used to separate nested namespaces instead of a dot. You’ll see this in the code file which follows.
  • To put your own code into a nested namespace, you’ll have to actually nest the namespace declarations. Separating several of them with double colons won’t work.
  • To declare a managed class, use the additional keyword ref.
  • Don’t forget the different use of the public keyword for the members in C++ compared to C#.
  • The ^ character denotes a reference to a managed object (stored on the managed heap), separating it from unmanaged pointers (* character).
  • Notice the interface IBind being implemented by the class. A standalone class wouldn’t need this of course. The virtual keyword in the method declaration would also be skipped in this case.

This is it for the header file. Let’s look at the code file now:

#include "MCPP.h"

using namespace System;
using namespace DamirsCorner::Samples::LateBinding;

String^ MCPP::Together(String^ first, String^ second)
{
   return String::Empty;
}

It’s pretty obvious the method doesn’t really do anything but it’s the skeleton we’re focusing on at the moment. You should turn your attention to the following:

  • The code file must include the header file it implements (i.e. defines its methods). To refresh your C++ knowledge: it’s a good habit to use quotes for internal project header files and pointy brackets for external library header files. The compiler looks in the project directory first for the former and in the include directories first for the latter. It checks the other location if it doesn’t find the file but it will work faster if you use the right one (not to mention that by doing it you’re avoiding the problem of locating the wrong include with the same name).
  • You can notice the double colons as namespace separators as I mentioned before.
  • The methods you’re implementing are not within the class declaration any more. You already declared the class in the header file therefore you only set the method class membership here by prefixing its name with the class name and a colon, just like you were calling a static method.
There’s a reason I used String for parameter and return value type. While value types can be directly used in unmanaged code, reference types have to be correctly marshaled. But that’s a complex enough topic to justify a separate posting. The only thing left for this one is to make the code above compile. Just start a new Class Library project in C++ (you’ll find it in the CLR group) and paste the code to the right files. After you reference the assembly containing the interface used (similar to doing it in C#) the code should build successfully.

5/14/2006 6:11:27 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

C++ project build from network share fails in VS2005 (Development | C++)

Trying to build a C++ project opened from a network share in Visual Studio 2005 might fail with a strange error: Command line error D8022 : cannot open '$(OutDir)\RSP00000115642624.rsp'. Double clicking it in the Error List window crashes the Visual Studio. The issue is reported in the MSDN Product Feedback Center.

Additional exploration and experimentation revealed that the problem only appears when the share host can’t authenticate the user reading from and writing to the share. This means there will be no error when the user and the share hosting computer are in the same domain. You can find a more thorough explanation in the following MSDN forum post.

Unfortunately this usually isn’t the case in the home environment. It’s best you just avoid the problem altogether by opening the project from a local drive. Just use a version control system or a synchronization tool to assure the same files are on the local machine and on the server when you need to.

5/14/2006 3:05:50 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Wednesday, May 03, 2006

Useful PInvoke resources (Development | .NET)

Completely unexpectedly I stumbled across Platform Invocation Services today when I was introduced to the IBM Tivoli Storage Manager API. It should have been a simple case of getting to know the API and writing a sample application with it, but it turned out that there’s no managed wrapper available for it (in spite of first being told otherwise). Combine that fact with an abundance of low level calls and large structs as their parameters and you can imagine that after one day there’s still a long way to that sample application, not to mention the final solution.

For anybody else like me out there who hasn’t done more than an occasional DllImport call or two the following resources should help getting to grips with the PInvoke basics:

  • Platform Invoke Tutorial – a must read for anyone starting with PInvoke. As usual, the follow-up links provide lots of additional useful information.
  • PINVOKE.NET – an indispensable source of information on WinAPI PInvoke calls. Not exactly what I needed but still the abundance of example calls turned out helpful.
  • The P/Invoke Wizard – a simple tool for converting C/C++ include files to PInvoke declarations. Not perfect and a bit pricy for what it does but still worth it due to the time it can save you when dealing with many large include files.
I hope you find these useful just as I did.

5/3/2006 10:34:25 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Blog Feeds
RSS 2.0 RSS 2.0
Atom 1.0 ATOM 1.0
Fellow Bloggers
 Andrej Tozon
 Dejan Sarka
 Dusan Zupancic
 Matevz Gacnik
 Miha Markic
Disclaimer
The content of this site are my own personal opinions and do not represent my employer's view in anyway. In addition, my thoughts and opinions often change, and as a weblog is intended to provide a semi-permanent point in time snapshot you should not consider out of date posts to reflect my current thoughts and opinions.

Powered by:
newtelligence dasBlog 1.8.5223.2

© 2008 Damir Arh, M. Sc. Send mail to the author(s)

Microsoft Certified Professional
Currently Reading
Currently Playing
Currently Watching