# How to cast FARPROC! to Func\<SomethingElse\>?

**URL:** https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701
**Category:** Silver
**Tags:** windows, silver
**Created:** [March 7, 2018, 2:04am UTC](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701 "2018-03-07T02:04:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![conio](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@conio](https://talk.remobjects.com/u/conio)
#### Post date: [March 7, 2018, 2:04am UTC](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701/1 "2018-03-07T02:04:03Z")

</div>

I’m trying to load a DLL with `LoadLibrary`, get an export using `GetProcAddress` and call it.

(This is basicaly the same problem as in [Calling functions exported by Dlls from Silver](https://talk.remobjects.com/t/calling-functions-exported-by-dlls-from-silver/15334))

The code looks something like:

```
var hSilverDLL1 = LoadLibrary("SilverDLL1.dll")
var pFoo = GetProcAddress(hSilverDLL1, "_Foo@0");
var pFoo2 = pFoo as Func<Bool>

```

This fails with:

```
error E559: Cannot safely cast "FARPROC!" to "Func<Bool>" did you mean to use "as!"?
error E129: Cannot cast from "FARPROC!" to "Func<Bool>!"

```

Using `as!` doesn’t help. The first error disappears but the second remains. Trying to pass through `LPVOID` like this:

```
var pFoo = GetProcAddress(hSilverDLL1, "_Foo@0");
var pFoo2 = pFoo as! rtl.LPVOID
var pFoo3 = pFoo2 as! Func<Bool>

```

doesn’t work either:

```
error E129: Cannot cast from "LPVOID!" to "Func<Bool>!"

```

(Using `as` rather than `as!` gives even more errors. like in the first excerpt.)

The suggestion is the post I linked above doesn’t work for me. I can’t statically linked to the DLL. I need to load it at runtime and discover its exports at runtime.

Am I missing some obvious way to perform this cast?

(Using Elements 10.0.0.2255 on VS 2015 on Windows 10 x64, compiling x86 binaries.)

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [March 7, 2018, 7:37am UTC](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701/2 "2018-03-07T07:37:46Z")

</div>

See

> [@Access Violation casting FARPROC to Any in Silver](https://talk.remobjects.com/t/access-violation-casting-farproc-to-any-in-silver/15702/2):
>
> The big problem here is that it shouldn’t allow this in the first place (same issue as [FARPROC to Func](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701)). Func and friends are “block” delegates, in that they capture a self pointer too and end up as a class (Delegate in Island/Echoes), which isn’t compatible with function pointers. We don’t currently have a syntax for function pointers in swift, but I see Swift now has one, so expect this one to work for fridays build: typealias MyFunc = @convention(c) () -\> Bool var pFoo = GetProcAddress(hSi…

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [March 7, 2018, 11:56am UTC](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701/3 "2018-03-07T11:56:05Z")

</div>

> [@ck](#):
>
> We don’t currently have a syntax for function pointers in swift,

[FunctionPointer](https://docs.elementscompiler.com/API/Aspects/FunctionPointer/) doesn’t work in Swift/Island?

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [March 7, 2018, 12:28pm UTC](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701/4 "2018-03-07T12:28:14Z")

</div>

That works too.

---

<div class="post-metadata">

### Author: ![conio](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@conio](https://talk.remobjects.com/u/conio)
#### Post date: [March 8, 2018, 2:28am UTC](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701/5 "2018-03-08T02:28:18Z")

</div>

Maybe I’m doing something wrong but I can’t get it to compile.

I’m doing something like (shamelessly adapted from the GUI sample):

```auto
import rtl

class Program {
    @FunctionPointer private typealias FooType = () -> (Bool)

    static let szTitle: LPCWSTR = "RemObjects Elements — Island Windows Sample"
    static let szWindowClass: LPCWSTR = "IslandWindowsSample"
    static var button: HWND = nil

    @CallingConvention(CallingConvention.Stdcall)
    static func WndProc(_ hWnd: HWND, _ message: UINT, _ wParam: WPARAM, _ lParam: LPARAM) -> Integer {
        switch message {
            case WM_COMMAND:
                if wParam == BN_CLICKED && lParam == button as! rtl.LPARAM {
                    MessageBox(hWnd, "You clicked, hello there!", szTitle, 0)
                    var x = true

                    var hSilverDLL1 = LoadLibrary("SilverDLL1.dll")
                    var pFoo = GetProcAddress(hSilverDLL1, "_Foo@0")
                    
                    var pFoo3 = pFoo as? FooType
                    x = pFoo3()

                    if (!x) { writeLn("fdg") }
                }
.....

```

I still get: `(E129) Cannot cast from "FARPROC!" to "Program.FooType?"`

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [March 8, 2018, 6:07am UTC](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701/6 "2018-03-08T06:07:45Z")

</div>

You need the upcoming beta (fridays) build.

---

<div class="post-metadata">

### Author: ![conio](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@conio](https://talk.remobjects.com/u/conio)
#### Post date: [March 19, 2018, 10:55pm UTC](https://talk.remobjects.com/t/how-to-cast-farproc-to-func-somethingelse/15701/7 "2018-03-19T22:55:36Z")

</div>

Thanks. I’ll try it out as soon as it gets to the public stable channel.

~~Will I be needing both `@FunctionPointer` mentioned here **and** `@convention(c)` mentioned in the other post or will only one of them be enough?~~  
Strike that. I see that in the other post you wrote that `@convention` alone should be enough.
