سؤال

I am trying to import dll in my delphi-prism program and never done it before. So, after finding some answer online, I put something together as follows but doesn't work.

  MyUtils = public static class
  private
    [DllImport("winmm.dll", CharSet := CharSet.Auto)]
    method timeBeginPeriod(period:Integer):Integer; external;
  protected
  public
    constructor;
  end;

Here is how I use it:

var tt := new MyUtils;
tt.timeBeginPeriod(1);

When I run my program, I keep getting the following errors.

  • "MyUtils" does not provide an accessible constructor.
  • "System.Object" does not contain a definition for "timeBeginPeriod" in expression "tt.timeBeginPeriod."

What am I doing wrong? How do you import dll in delphi-prism?

I followed this stackoverflow question - Delphi Prism getting Unknown Identifier "DllImport" error

هل كانت مفيدة؟

المحلول

You're very close.

You don't need the constructor, so you can remove it:

MyUtils = public static class
private
  [DllImport("winmm.dll", CharSet := CharSet.Auto)]
  method timeBeginPeriod(period:Integer):Integer; external;
protected
public
end;

If you're calling the timeBeginPeriod function from outside the unit where it's declared, you need to change it's visibility to public.

You also don't need to create an instance to call the function:

MyUtils.timeBeginPeriod(1);

I tested this with an app that declared and used SendMessage instead, so I could easily check to make sure it actually worked (I sent an EM_SETTEXT message to an Edit control on the same form).

نصائح أخرى

  MyUtils = public static class
  public
    [DllImport("winmm.dll", CharSet := CharSet.Auto)]
    class method timeBeginPeriod(period:Integer):Integer; external;
  end;


MyUtils.timeBeginPeriod(1);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top