all groups > dotnet clr > june 2005 >
You're in the

dotnet clr

group:

String.Trim method throws ExecutionEngineException


String.Trim method throws ExecutionEngineException Eugene
6/16/2005 1:07:03 PM
dotnet clr:
This problem occurs after system has been fully initialized and main form is
displayed. Calling 'Trim' method during initialization causes no problem.

I tried to avoid using this method but some framework classes use it as
well. For example 'DataView.Sort' property uses it according to call stack
window.

Please help who knows what's going on.

Thank you,

Eugene
Re: String.Trim method throws ExecutionEngineException Jon Skeet [C# MVP]
6/16/2005 9:22:53 PM
[quoted text, click to view]

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: String.Trim method throws ExecutionEngineException Eugene
6/17/2005 11:13:03 AM
Jon,

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?

Please advice.

Thank you once again for taking time to reply to me.

Eugene

[quoted text, click to view]
Re: String.Trim method throws ExecutionEngineException Jon Skeet [C# MVP]
6/18/2005 10:16:01 AM
[quoted text, click to view]

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
Re: String.Trim method throws ExecutionEngineException - Solved Eugene
6/20/2005 11:41:07 AM
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]
Re: String.Trim method throws ExecutionEngineException - Solved Jon Skeet [C# MVP]
6/20/2005 10:17:12 PM
[quoted text, click to view]

<snip>

[quoted text, click to view]

Well, I don't know much about GetPrivateProfileString myself, but I
suspect that .NET thought it "owned" the memory which was being
referenced by your out parameter. I believe that *usually* when you
need to effectively get a string as an out parameter, you should pass
in a StringBuilder, but I'm afraid I'm really not an interop expert.

Sorry to pass the buck like this, but it's much better that you get
advice from someone who definitely knows their way around P/Invoke
better than me - I'd suggest asking on the .interop group.

You might also want to look at
http://www.codeproject.com/csharp/cs_ini.asp

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button