title: How to dynamically load and call a function in a BPL package
body: <p>It seems pretty straightforward but the code below doesn't work.</p>
  
  <p>BPL:</p>
  
  <pre><code>procedure DoSomething();
  begin
    LogEvent('Did');
  end;
  
  exports
    DoSomething;
  </code></pre>
  
  <p>Main EXE:</p>
  
  <pre><code>procedure CallModuleFunc;
  var
    H: THandle;
    P: procedure();
  begin
    H := LoadPackage('mymod.bpl');
    try
      if (H &lt;&gt; 0) then
      begin
        @P := GetProcAddress(H, 'DoSomething');
        if Assigned(P) then
          P();
      end;
    finally
      UnloadPackage(H);
    end;
  end;
  </code></pre>
  
  <p>Now there's no error, the bpl loads successfully with <code>LoadPackage()</code> but <code>GetProcAddress()</code> returns nil. Why? probably because name mangling. I've tried adding <code>stdcall</code> (both exported function and <code>P</code>'s declaration) but that didn't solve the problem. I've seen hundreds of examples on the web that it's supposed to be working this way. I even tried <code>GetProcAddress(H, 'DoSomething$qqsv')</code> but it didn't work either. What am I missing here?</p>