Jon,
I have solved my problem by inserting string strA = strB.Trim( ) statement
between several lines of code until I have pinpointed exact place where
"Trim( )" method starts failing.
The reason was an incorrect declaration of "GetPrivateProfileString" API -
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true,
EntryPoint = "GetPrivateProfileString", CallingConvention =
CallingConvention.StdCall )]
public extern static int GetPrivateProfileString( string section, string
key, string default, [Out, MarshalAsAttribute(UnmanagedType.LPTStr)] string
buffer, ref int size, string fileName );
which I used in the following way -
string fileName = "C:\\Windows\\file.ini";
string buffer = String.Empty;
int size = 0;
size = GetPrivateProfileString( "Section", "Key", "Default", buffer, ref
size, fileName );
version = new string( '\0', size );
GetPrivateProfileString( "Section", "Key ", "Default", buffer, ref size,
fileName );
After the last call the "buffer" variable did contain correctly read value
but all subsequent calls to String.Trim( ) method began to throw
‘ExecutionEngineException’.
Now I am using the following declaration -
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true,
EntryPoint = "GetPrivateProfileString", CallingConvention =
CallingConvention.StdCall )]
public extern static int GetPrivateProfileString( string section, string
key, string default, IntPtr buffer, ref int size, string fileName );
I would appreciate if you briefly explained what was wrong with the
declaration I had used in the first place.
Thank you for your help.
Eugene.
[quoted text, click to view] "Jon Skeet [C# MVP]" wrote:
> Eugene <Eugene@discussions.microsoft.com> wrote:
> > Thank you for your reply. I will prepare short program after the weekends.
> >
> > For now I have a feeling that this problem is related to the one described
> > in the following bug article -
> >
> >
http://support.microsoft.com/default.aspx?scid=kb;en-us;327416 > >
> > Although I do not use Activator.CreateInstance() method but I do use this
> > approach -
> >
> > string file_name = ...
> > Type target_type = ...
> > System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(
> > file_name );
> > object oCreated = assembly.CreateInstance( target_type, true );
> >
> > Would you agree with my conclusion that Activator.CreateInstance() and
> > Assembly.CreateInstance() execute the same routine therefore the fix
> > Microsoft may have for the bug described in the article I have referred to
> > above is applicable in my case as well?
>
> I think it's unlikely to be connected, for a few reasons:
>
> 1) That KB article applies to the 1.0 version of the framework.
> 2) Unless you're creating an array type that way, I don't think the KB
> article is actually relevant.
> 3) I would expect the exception to be thrown immediately, whereas you
> said before that you were having problems with String.Trim, rather
> than Assembly.CreateInstance.
>
> (I hope the code above isn't actually representative of your real code
> though - it would suggest that you're asking the assembly to create a
> type which isn't from it. You should be loading the assembly, then
> fetching the type from it, then creating an instance.)
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too