-2

I've being trying all day following some tutorials and many answers about this subject, but cant figure what I'm doing wrong. Delphi won't even compile, I`ll need to sign some XML docs and i thought a ref or an out string parameter would be the best way to do so. Thanks in advance for any help.

DLL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Xml;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

    namespace Test
    {
        public class Test
        {

            [DllExport(CallingConvention = CallingConvention.StdCall)]
            [return: MarshalAs(UnmanagedType.LPWStr)]
            [ComVisible(true)]

            public static void ShowCertificatesList([MarshalAs(UnmanagedType.BStr)] ref string pXmlDoc)
            {
                pXmlDoc = "test";
                var store = new X509Store(StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadOnly);
                var certificates = store.Certificates;
                X509CertificateCollection vListaCertificados;

                vListaCertificados = X509Certificate2UI.SelectFromCollection(certificates, "test", "Certificate list", X509SelectionFlag.SingleSelection);
            }
        }
    }

it works from Windows form:

private void button1_Click(object sender, EventArgs e)
    {
        string TestStr= "123";
        Test.Test.ShowCertificatesList(ref TestStr);


    }

But not from Delphi:

 var
   Form3: TForm3;

    procedure ShowCertificatesList(var pStrRef : String) ; stdcall; external 'Test.dll' name 'ShowCertificatesList';

    implementation

    {$R *.dfm}

  procedure TForm3.Button1Click(Sender: TObject);
    var
    vStrRef: String;
    begin
      vStrRef:=  'in' ;
      ShowCertificatesList(vStrRef);
      vStrRef := vStrRef;
  end;
Marisco
  • 125
  • 1
  • 3
  • 16
  • You've not explained what the problem is you're having with the Delphi version. *But not from Delphi* is not a meaningful problem description. What **specific problem** are you having with the Delphi code? (Other than the fact you're using a Delphi string, which is not interop, and you're passing it as a var. You've also said that the DLL function will be returning `ManagedType.LPWStr`, but defined the function itself as returning `void`. It's not clear from the DLL code exactly what you're trying to accomplish, and the Delphi code that sets `vStrRef := vStrRef;` is a noop.) – Ken White Mar 29 '16 at 01:51
  • Sory about that, Delphi won't even compile. – Marisco Mar 29 '16 at 01:54
  • That's not a meaningful problem description either. What **specific error** are you getting? If Delphi won't compile it, it provides error messages that tell you why. They're right on your screen in front of you, and you can copy/paste using the right-click menu from the Messages tab. There is zero reason for you to fail to include that information in your question. You're asking for **free help** to solve **your problem**; you should make it as easy as you can for us to do so by providing the details you already have available. – Ken White Mar 29 '16 at 01:58
  • Its just a sample Ken, to simplify the means, alsp the ManagedType.LPWStr and var on delphi are from trying some answers like this one http://stackoverflow.com/questions/20521060/calling-a-net-dll-from-delphi2006-to-show-a-wpf-form – Marisco Mar 29 '16 at 02:01
  • no error is lauched from debug on delphi – Marisco Mar 29 '16 at 02:02
  • If it's running, then Delphi will compile it. You can't run code that won't compile. I'm voting to close this question until you edit to make it clear what you're asking, because up to now you have not despite repeated requests for you to do so. – Ken White Mar 29 '16 at 02:04
  • its not running at all at the end it just doenst complie and no error at all – Marisco Mar 29 '16 at 02:07
  • That's nonsense. If it won't compile, you get compiler errors. If you get *no error is launched from debug*, it's compiled and executing. Make up your mind. Either it doesn't compile (in which case you need to **post the compiler errors**) or it compiles and you're getting no errors in the debugger. I've been using Delphi since version 1.0, and I've **never** had code that wouldn't compile that did not give me compiler errors to say why. You can't have it both ways. It's either/or. Which one? – Ken White Mar 29 '16 at 02:10
  • check this out http://stackoverflow.com/questions/13330890/strange-when-i-call-function-from-dll-application-not-start-but-no-error-found – Marisco Mar 29 '16 at 02:17
  • Nice. You've posted another bad question. You've given no information here about the issue with your Delphi code. You can't even say if it compiles or not, and you're ignoring all requests to clarify the problem. If you can't decide whether it compiles or runs, how do you expect us to help you? **It either does not compile, in which case you get compiler errors, or it compiles and runs but doesn't work, in which case you need to explain how it's not working.** Those are the only two options, and you **must choose one of them**. There is no third option available. – Ken White Mar 29 '16 at 02:24
  • there is, I know its weird, but you dont have to repeatyour self, if u did read the post above, delphi does compile successfully but the form wont show up... it just doesnt, but thanks anyway. – Marisco Mar 29 '16 at 02:29
  • I did *read the post above*, where it clearly says *Delphi won't even compile*. Suit yourself. I've asked repeatedly for you to edit the question to clarify the problem, and you still refuse to cooperate. Suit yourself. I've wasted enough time here. Good luck. – Ken White Mar 29 '16 at 02:32
  • Its driving me crazy too, just trying open the certificate window from a c# dll. if you have a better idea. – Marisco Mar 29 '16 at 02:34
  • The problem that @Ken has is that you have said the following two statements: "does not compile" and "does compile". That is a direct contradiction. Clearly you meant something else when you said "does not compile". Describing the problem carefully and methodically is important to help you (and us) understand the problem. – David Heffernan Mar 29 '16 at 08:55

2 Answers2

0

Just to indicate an alternative approach.

You can always manipulate Windows certificate store directly via native API (.NET also uses such), as revealed in blog posts such as,

http://vanillasky-room.cocolog-nifty.com/blog/2013/10/cryptoapi-and-c.html (in Japanese, but Google can translate it to English).

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • we do have the program build with delphi 2009 but during my research we found capcom was discontinued, and we had many exceptions singning with A3 certificate. same didnt happen using c# so we are trying to find a way out. – Marisco Mar 29 '16 at 03:05
  • also the caps 'ID" and "id" problem is forcing me. – Marisco Mar 29 '16 at 03:07
  • @Marisco, changed the link to a more recent one. Microsoft does publish new APIs to work with new standards. You might have to wrap the new native APIs on your own, or use the third party translation. – Lex Li Mar 29 '16 at 03:11
  • Thank you much! I'll check it out! – Marisco Mar 29 '16 at 03:14
0

The Delphi function is declared incorrectly. Use WideString to match BStr:

procedure ShowCertificatesList(var pStrRef: WideString);
  stdcall; external 'Test.dll';

The following attributes should be removed from the C# code:

[return: MarshalAs(UnmanagedType.LPWStr)]
[ComVisible(true)]

The function has no return value and you aren't using COM.

If it were me I'd use native Windows APIs.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490