CharToOem - error

When I use the CharToOem function it raise a Kernel32.dll access violation.

My sourcecode looks something like this.

  text : string;

  CharToOem (PChar(text),PChar(text));

How can I prevent the error?

Thanking you ín anticipation
Preben Holm
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Preben,

have you allocated memory for the text? Although I do a lot with languages and character conversion I still don't know what they mean with OEM charset. Can it be that the resulting text is larger than the original text (keyword: MBCS)?

Ciao, Mike
This code from RX Library

function StrToOem(const AnsiStr: string): string;
  Src, Dest: array[0..255] of Char;
  SetLength(Result, Length(AnsiStr));
  if Length(Result) > 0 then
    CharToOem(PChar(AnsiStr), PChar(Result));
    StrPCopy(Src, AnsiStr);
    AnsiToOem(Src, Dest);
    Result := StrPas(Dest);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hi Preben,

I don't know why you're getting an exception. I'm doing the same as you do since several years without any problems. Have you tried it on several computers? Perhaps your Windows installation is corrupt? Or do you give in mega strange text?   :-)


this function converts dos texts into Windows texts. E.g. if you read in autoexec.bat, you see strange characters for "ö, ü, ä". If you use OemToChar before showing the text everything is fine.

Regards, Madshi.
Thanks Madshi, but it is still irritating because it is not clear that the DOS code page is meant when speaking of an OEM code page. But obviously it is so...

Ciao, Mike
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.