Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simphony 2 Load Dll through ISL code written in C# 1

Status
Not open for further replies.

nvak

Programmer
May 25, 2015
60
GR
Hello guys i need your help.

I am trying to load a Dll written in C# through an isl code on Micros Simphony 2.

My problem is that i can not call the method that there is in the dll.

I successfully load the dll but i can not run the method.

The error is "Dll Undefined Function" on line 7.
I have already tried the DLLCall, DLLCall_Cdecl, DLLCall_STDCall and DLLCallW keywords.

Can anyone help me?

Code:
VAR result : N9
VAR dll_handle : N9

DLLLoad dll_handle, "C:\micros\simphony\WebServer\NickTest.dll"
	
IF dll_handle <> 0
	DLLCall dll_handle, Test( ref result)
ENDIF

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NickTest
{
    public class Class1
    {
        public void Test(ref int result)
        {
            result = 2;
       }
    }
}
 
You're going about that the wrong way. While Simphony 2 does support writing code through C#, it is through extensions only. SIM still requires native DLL's in order to do calls like that.
 
thanks for your answer.

Native DLL's like a C or C++ DLL?
 
I've never tested this, because of the realization that COM ports get completely locked by Micros when in use, but something like this in C++ should work. Also, these are a pain in the ass, big time, to try and get compiled for a windows CE device. You're on Simphony 2 so you really shouldn't have any, but if you do...

Main.h file:

Code:
[COLOR=#808080]#ifndef[/color] __MAIN_H__
[COLOR=#808080]#define[/color] [COLOR=#6F008A]__MAIN_H__[/color]
 
[COLOR=#808080]#include[/color] [COLOR=#A31515]&lt;windows.h&gt;[/color]
 
[COLOR=#008000]/*  To use this exported function of dll, include this header[/color]
[COLOR=#008000] *  in your project.[/color]
[COLOR=#008000] */[/color]
 
[COLOR=#808080]#ifdef[/color] BUILD_DLL
    [COLOR=#808080]#define[/color] DLL_EXPORT [COLOR=#0000FF]__declspec[/color]([COLOR=#0000FF]dllexport[/color])
[COLOR=#808080]#else[/color]
    [COLOR=#808080]#define[/color] [COLOR=#6F008A]DLL_EXPORT[/color] [COLOR=#0000FF]__declspec[/color]([COLOR=#0000FF]dllimport[/color])
[COLOR=#808080]#endif[/color]
 
 
[COLOR=#808080]#ifdef[/color] [COLOR=#6F008A]__cplusplus[/color]
[COLOR=#0000FF]extern[/color] [COLOR=#A31515]&quot;C&quot;[/color]
{
[COLOR=#808080]#endif[/color]
 
[COLOR=#0000FF]void[/color] [COLOR=#6F008A]DLL_EXPORT[/color] SetComPort([COLOR=#0000FF]int[/color] comPort, [COLOR=#0000FF]int[/color] buadRate, [COLOR=#0000FF]int[/color] parityType, [COLOR=#0000FF]int[/color] dataBits, [COLOR=#0000FF]int[/color] stopBits);
[COLOR=#0000FF]void[/color] [COLOR=#6F008A]DLL_EXPORT[/color] PrintQRCode([COLOR=#0000FF]char[/color]* data, [COLOR=#0000FF]int[/color] fandc);
 
[COLOR=#808080]#ifdef[/color] [COLOR=#6F008A]__cplusplus[/color]
}
 
[COLOR=#808080]#endif[/color]
 
[COLOR=#808080]#endif[/color] [COLOR=#008000]// __MAIN_H__[/color]

Main.cpp file:

Code:
[COLOR=#808080]#include[/color] [COLOR=#A31515]&quot;main.h&quot;[/color]
[COLOR=#808080]#include[/color] [COLOR=#A31515]&quot;stdio.h&quot;[/color]
 
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] cPort = 1;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] bRate = 9600;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] pType = 0;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] dBits = 8;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] sBits = 1;
[COLOR=#008000]// a sample exported function[/color]
 
[COLOR=#0000FF]void[/color] [COLOR=#6F008A]DLL_EXPORT[/color] SetComPort([COLOR=#0000FF]int[/color] [COLOR=#808080]comPort[/color], [COLOR=#0000FF]int[/color] [COLOR=#808080]baudRate[/color], [COLOR=#0000FF]int[/color] [COLOR=#808080]parityType[/color], [COLOR=#0000FF]int[/color] [COLOR=#808080]dataBits[/color], [COLOR=#0000FF]int[/color] [COLOR=#808080]stopBits[/color])
{
    cPort = [COLOR=#808080]comPort[/color];
    bRate = [COLOR=#808080]baudRate[/color];
    pType = [COLOR=#808080]parityType[/color];
    dBits = [COLOR=#808080]dataBits[/color];
    sBits = [COLOR=#808080]stopBits[/color];
}
 
[COLOR=#0000FF]void[/color] [COLOR=#6F008A]DLL_EXPORT[/color] PrintQRCode([COLOR=#0000FF]char[/color]* [COLOR=#808080]data[/color], [COLOR=#0000FF]int[/color] [COLOR=#808080]fandc[/color])
{
    [COLOR=#0000FF]char[/color] comFile&#91;32&#93; = [COLOR=#A31515]&quot;\\\\.\\COM&quot;[/color];
    [COLOR=#0000FF]char[/color] scratch&#91;10&#93;;
    sprintf(scratch, [COLOR=#A31515]&quot;%d&quot;[/color], cPort);
    strcat(comFile, scratch);
 
    [COLOR=#2B91AF]HANDLE[/color] hComm;
hComm = [COLOR=#6F008A]CreateFile[/color](
comFile,
[COLOR=#6F008A]GENERIC_READ[/color] | [COLOR=#6F008A]GENERIC_WRITE[/color],
0,
0,
[COLOR=#6F008A]OPEN_EXISTING[/color],
[COLOR=#6F008A]FILE_FLAG_OVERLAPPED[/color],
0
);
 
[COLOR=#2B91AF]DCB[/color] dcb = {0};
[COLOR=#0000FF]if[/color] (GetCommState(hComm, &amp;dcb))
    {
        dcb.BaudRate = bRate;
        dcb.ByteSize = dBits;
        [COLOR=#0000FF]switch[/color] (pType)
        {
            [COLOR=#0000FF]case[/color] 2:
                dcb.Parity = [COLOR=#6F008A]EVENPARITY[/color];
                [COLOR=#0000FF]break[/color];
            [COLOR=#0000FF]case[/color] 1:
                dcb.Parity = [COLOR=#6F008A]ODDPARITY[/color];
                [COLOR=#0000FF]break[/color];
            [COLOR=#0000FF]case[/color] 0:
                dcb.Parity = [COLOR=#6F008A]NOPARITY[/color];
                [COLOR=#0000FF]break[/color];
            [COLOR=#0000FF]default[/color]:
                dcb.Parity = [COLOR=#6F008A]NOPARITY[/color];
        }
        dcb.StopBits = sBits - 1;
    }
 
    [COLOR=#2B91AF]OVERLAPPED[/color] osWrite = {0};
    [COLOR=#2B91AF]DWORD[/color] dwWritten;
[COLOR=#2B91AF]DWORD[/color] dwRes;
[COLOR=#2B91AF]BOOL[/color] fRes;
 
[COLOR=#2B91AF]byte[/color] storeQRCode&#91;&#93; = {
0x1D, 0x28, 0x6B, ([COLOR=#2B91AF]byte[/color])(strlen([COLOR=#808080]data[/color]) + 3), 0x00, 0x31, 0x50, 0x30
};
 
[COLOR=#2B91AF]byte[/color] modSizeCenterPrintCode&#91;&#93; = {
0x1D, 0x28, 0x6B, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00,
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x43, 0x07,
0x1b, 0x61, 0x01,
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x51, 0x30
};
 
[COLOR=#2B91AF]byte[/color] feedAndCut&#91;&#93; = { 27, 64, 0xA, 0x1d, 0x56, 66, 30 };
 
[COLOR=#2B91AF]byte[/color] dataToPrint&#91; 8 + strlen([COLOR=#808080]data[/color]) + 28 + 7 &#93;;
memcpy(dataToPrint, storeQRCode, 8);
memcpy(dataToPrint + 8, [COLOR=#808080]data[/color], strlen([COLOR=#808080]data[/color]));
memcpy(dataToPrint + 8 + strlen([COLOR=#808080]data[/color]), modSizeCenterPrintCode, 28);
[COLOR=#0000FF]if[/color] ([COLOR=#808080]fandc[/color] == 1)
        memcpy(dataToPrint + 8 + strlen([COLOR=#808080]data[/color]) + 28, feedAndCut, 7);
 
    osWrite.hEvent = [COLOR=#6F008A]CreateEvent[/color]([COLOR=#6F008A]NULL[/color], [COLOR=#6F008A]TRUE[/color], [COLOR=#6F008A]FALSE[/color], [COLOR=#6F008A]NULL[/color]);
 
    [COLOR=#0000FF]if[/color] (!WriteFile(hComm, dataToPrint, 8 + strlen([COLOR=#808080]data[/color]) + 28, &amp;dwWritten, &amp;osWrite))
    {
        [COLOR=#0000FF]if[/color] (GetLastError() != [COLOR=#6F008A]ERROR_IO_PENDING[/color])
        {
            fRes = [COLOR=#6F008A]FALSE[/color];
        }
        [COLOR=#0000FF]else[/color]
        {
            dwRes = WaitForSingleObject(osWrite.hEvent, [COLOR=#6F008A]INFINITE[/color]);
            [COLOR=#0000FF]switch[/color] (dwRes)
            {
            [COLOR=#0000FF]case[/color] [COLOR=#6F008A]WAIT_OBJECT_0[/color]:
                [COLOR=#0000FF]if[/color] (!GetOverlappedResult(hComm, &amp;osWrite, &amp;dwWritten, [COLOR=#6F008A]FALSE[/color]))
                    fRes = [COLOR=#6F008A]FALSE[/color];
                [COLOR=#0000FF]else[/color]
                    fRes = [COLOR=#6F008A]TRUE[/color];
                [COLOR=#0000FF]break[/color];
            [COLOR=#0000FF]default[/color]:
                fRes = [COLOR=#6F008A]FALSE[/color];
                [COLOR=#0000FF]break[/color];
            }
        }
    }
    [COLOR=#0000FF]else[/color]
        fRes = [COLOR=#6F008A]TRUE[/color];
}
 
[COLOR=#0000FF]extern[/color] [COLOR=#A31515]&quot;C&quot;[/color] [COLOR=#6F008A]DLL_EXPORT[/color] [COLOR=#2B91AF]BOOL[/color] [COLOR=#6F008A]APIENTRY[/color] DllMain([COLOR=#2B91AF]HINSTANCE[/color] [COLOR=#808080]hinstDLL[/color], [COLOR=#2B91AF]DWORD[/color] [COLOR=#808080]fdwReason[/color], [COLOR=#2B91AF]LPVOID[/color] [COLOR=#808080]lpvReserved[/color])
{
    [COLOR=#0000FF]switch[/color] ([COLOR=#808080]fdwReason[/color])
    {
        [COLOR=#0000FF]case[/color] [COLOR=#6F008A]DLL_PROCESS_ATTACH[/color]:
            [COLOR=#008000]// attach to process[/color]
            [COLOR=#008000]// return FALSE to fail DLL load[/color]
            [COLOR=#0000FF]break[/color];
 
        [COLOR=#0000FF]case[/color] [COLOR=#6F008A]DLL_PROCESS_DETACH[/color]:
            [COLOR=#008000]// detach from process[/color]
            [COLOR=#0000FF]break[/color];
 
        [COLOR=#0000FF]case[/color] [COLOR=#6F008A]DLL_THREAD_ATTACH[/color]:
            [COLOR=#008000]// attach to thread[/color]
            [COLOR=#0000FF]break[/color];
 
        [COLOR=#0000FF]case[/color] [COLOR=#6F008A]DLL_THREAD_DETACH[/color]:
            [COLOR=#008000]// detach from thread[/color]
            [COLOR=#0000FF]break[/color];
    }
    [COLOR=#0000FF]return[/color] [COLOR=#6F008A]TRUE[/color]; [COLOR=#008000]// succesful[/color]
}
 
Moregelen, Thats incorrect. Simphony 2 supports using .NET DLLs directly via SIM however the LoadDll and DllCall are still reserved for native code and not for .NET Dll's.

Here is a very simple SIM & C# Class

Code:
// MyTest.dll
namespace MyTest
{
    public class MyTestClass
    {
         public void SayHello()
         {
             System.Windows.Forms.MessageBox.Show("Hello");
         }

         public string ReturnAString()
         {
              return "hello";
         }
    }
}

Code:
NetImport from &"D:\MyDirectory\MyTest.dll"    // C# Dll

Event Inq : 1

    var MyVarName1:object = new MyTestClass()   // Assumes there is no namespace conflict.
    var MyVarName2:object = new MyTest.MyTestClass()   // Specifically states the namespace.
    var MyVarName3:A

    MyVarName1.SayHello()
    MyVarName2.SayHello()
    MyVarName3 = MyVarName1.ReturnAString()

EndEvent

Untested code. Written from memory.



Do you want some custom SIM scripts developed. Contact me via my website
 
Oh and also SIM for Simphony 2 has a nice TRY/Catch system when you are using .NET dlls

Code:
Event Inq : 1

    try
        Call DoSomethingWithADll()    // An exception is thrown
    catch ex
        // Two string variables Message and Details can be accessed
        infomessage ex.Message
        infomessage ex.Details
    endtry

EndEvent

Simphony 2 SIM is very powerful but as usual nothing is documented.

Do you want some custom SIM scripts developed. Contact me via my website
 
I WANT TO KISS YOU AND HUG YOU
 
Very nice. Thanks. All I've done with Simphony 2 so far is write a gift card driver so I was making assumptions. Very useful to know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top