Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6033811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:32:17+00:00 2026-05-23T05:32:17+00:00

When I execute CryptFindLocalizedName more than once from my .Net code it comes back

  • 0

When I execute CryptFindLocalizedName more than once from my .Net code it comes back with invalid information. The first call is accurate but any subsequence calls using the same string returns bad data. I am not an expert on using the Win32 api in C# so I could be doing something wrong.

Here is my code…

[DllImport("cryp32.dll", CharSet = CharSet.Auto]
public static extern string CryptFindLocalizedName(
  [In] string pwszCryptName
);

public static void Test()
{
    Console.WriteLine(CryptFindLocalizedName("My"));   // Returns "Personal"
    Console.WriteLine(CryptFindLocalizedName("My"));   // Returns "<weirdchar>ersonal"
}

I am trying to return the Friendly Names to the certificate stores.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:32:18+00:00Added an answer on May 23, 2026 at 5:32 am

    The problem is presumably that the marshaller is doing something it should not with the return value. The MSDN documentation states:

    The returned pointer must not be freed.

    But the C# marshaller doesn’t know that. Instead you’ll need to take control over the marshalling of the return value.

    [DllImport("cryp32.dll", CharSet = CharSet.Auto]
    public static extern IntPtr CryptFindLocalizedName(
      string pwszCryptName
    );
    
    public static void Test()
    {
        IntPtr retval = CryptFindLocalizedName("My");
        string name = Marshal.PtrToStringUni(retval);
    }
    

    For simplicity I’ve ignored error handling, but this is the essence of your problem.

    The documentation for PtrToStringUni states:

    PtrToStringUni is useful for custom marshaling or for use when mixing managed and unmanaged code. Because this method creates a copy of the unmanaged string’s contents, you must free the original string as appropriate.

    In other words, it doesn’t free the return value from CryptFindLocalizedName and that’s exactly what you want in this situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to execute MSBuild from a NAnt script (which is called by CruiseControl.NET).
Is it possible to execute the Javascript in a PAC file from a .Net
I'd like to execute JavaScript code from within a C# assembly and have the
How to execute a query in MS Access db from Excel VBA code or
cursor.execute(SELECT user_id FROM myapp_location WHERE\ GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) < %s\ ,(user_utm_easting, user_utm_northing, 500)); This
To execute my application in iPhone first of all I have to add provisioning
How do I execute the Copy Web Site command for an ASP.NET project in
Please execute the following queries first to set up so that you can help
First execute these tables and data dumps :- CREATE TABLE IF NOT EXISTS `Tags`
I execute the following code via CLR, is there a reason why the message

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.