Calling DLL coded in D from Java

2015-06-16 Thread DlangLearner via Digitalmars-d-learn
I'd like to know if it is possible to call an DLL coded in D from 
Java? I don't have any knowledge on DLL calling mechanism, so I 
am wondering if this is possible or any special procedure should 
be followed.


I indeed tried a small example but didn't succeed. First I 
created an DLL from the template in VisualD as below:


import std.c.windows.windows;
import core.sys.windows.dll;
import std.c.stdio;

__gshared HINSTANCE g_hInst;

extern(C)

export void dllprint() {
printf("hello dll world\n");
}

extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID 
pvReserved)

{
switch (ulReason)
{
case DLL_PROCESS_ATTACH:
g_hInst = hInstance;
dll_process_attach(hInstance, true);
break;

case DLL_PROCESS_DETACH:
dll_process_detach(hInstance, true);
break;

case DLL_THREAD_ATTACH:
dll_thread_attach(true, true);
break;

case DLL_THREAD_DETACH:
dll_thread_detach(true, true);
break;

default:
break;
}
return true;
}

and its DEF file:

LIBRARY  "z_dll.dll"
EXETYPE NT
SUBSYSTEM WINDOWS
CODE SHARED EXECUTE
DATA WRITE
EXPORTS
dllprint

I am able to call the generated DLL from D. For calling from 
Java, I used JNA, and the code is:


public class TestDlangDLL {
public interface DlangDLL extends Library {

DlangDLL INSTANCE = (DlangDLL) Native.loadLibrary("C:\\z_dll",
DlangDLL.class);

void dllprint();
}

public static void main(String[] args) {
DlangDLL dDLL = DlangDLL.INSTANCE;
System.out.println(dDLL.toString()); //fine here
dDLL.dllprint(); //crash on this call
}
}

This test program is able to load the DLL, but unable to call the 
exported function dllprint.


Any guidance is appreciated.



Replacement of std.stream

2015-06-27 Thread DlangLearner via Digitalmars-d-learn
I will convert a Java program into D. The original Java code is 
based on the class RandomeAccessFile which essentially defines a 
set of methods for read/write Int/Long/Float/String etc. The 
module std.stream seems to be a good fit for this job, but in its 
documentation, it is marked deprecated. So I'd like to know what 
is the replacement for this module. If I don't use this module, 
what is the other apporach I should use. Thanks for help.


How to convert byte array to float

2015-07-17 Thread DLangLearner via Digitalmars-d-learn
Excuse me for my trivial question, but I'd like to know how to 
convert byte array to float? What I could think of are 
cast(float)(byte[]) and to!float(byte[]) but they do not work for 
me. Thanks!


Re: How to convert byte array to float

2015-07-17 Thread DLangLearner via Digitalmars-d-learn

On Friday, 17 July 2015 at 18:58:33 UTC, byron wrote:

On Friday, 17 July 2015 at 18:53:24 UTC, byron wrote:

On Friday, 17 July 2015 at 18:43:27 UTC, DLangLearner wrote:
Excuse me for my trivial question, but I'd like to know how 
to convert byte array to float? What I could think of are 
cast(float)(byte[]) and to!float(byte[]) but they do not work 
for me. Thanks!


You want to include [] in the cast so:



byte[] b = [1, 2, 3, 4]
byte[] f = cast(float[])b;

writeln(b);
writeln(f);

You can even cast on a slice:

byte[] b = [1, 2, 3, 4, 5, 6, 7, 8]
byte[] f = cast(float[])b[4..8];


Ah I miss read, if you want as a float, not a float array you 
can do:


byte[] b = [1, 2, 3, 4];
float f = *cast(float*)b.ptr;

not sure if there is a better way


thanks for quick reply, i'll plug above lines into my codes.


Re: I'll like to learn D

2015-09-19 Thread DlangLearner via Digitalmars-d-learn

On Saturday, 19 September 2015 at 17:42:50 UTC, uNknow123 wrote:
On Saturday, 19 September 2015 at 15:09:38 UTC, WhatMeWorry 
wrote:
On Saturday, 19 September 2015 at 13:41:03 UTC, uNknow123 
wrote:
Hi! I'll like to learn D Lang. I knew some Pawn, it is pretty 
similar, but not so similar, if you understan me. In Pawn we 
have to write just some words, and the Plugin is done, why 
Plugin, 'cuse Pawn = Scripting for Cs 1.6 and Sa:Mp. So, I am 
a rookie, can you help me please?


That is a very open ended question.  I'd start with 
http://ddili.org/ders/d.en/index.html
It will get your feet wet and you can wade in as deep as you 
want.



Thanks man, and there is'nt any interactive website?! Like 
CodeAcademy just for be a better proggramer, thanks anyway man! 
I'll read that book!


http://www.tutorialspoint.com/d_programming/index.htm


Compile time and runtime grammars

2015-10-10 Thread DLangLearner via Digitalmars-d-learn
Only now I found that most of my confusions are with D's compile 
time grammar or features. As an excuse, my confusions can be 
partially attributed to the way D is presented:


1. There are confusing keywords:
For example, there is a "if", there is also a "static if", there 
is a "if", and there is an "is()". For new learners like me, they 
cause confusion at least uneasiness.


2. Compile time grammar spreads among runtime grammar
Most documents present D's compile time grammar and runtime 
grammar in the same time. It made me feel that D's grammar is not 
consistent because compile time grammar seem to be exceptions 
from runtime grammar. If a document talks exclusively about 
runtime grammar first, and
introduces compile time grammar late, I think this will make 
readers accept those seemingly conflicting grammar. In fact 
without introducing compile time grammar, D is much similar to 
other languages, in this way the readers from other languages can 
find D more friendly.


With the understanding of D's compile time grammar, I can read D 
codes from other projects such as std packages, but I am still 
not easy about the way that D's compile time codes are not 
clearly distinguished from runtime codes. I am wondering if it is 
a good idea to clearly indicate those compile time codes with a 
special identifier say "@ct", or prefix "__" as in __traints, if 
so then those "inconsistencies" can be resolved as follows:


static if -> @ct if
static assert" -> @ct assert
enum fileName = "list.txt" -> @ct  fileName = "list.txt"
is (string[void]) -> @ct is (string[void])
mixin(`writeln("Hello World!");`) -> @ct `writeln("Hello 
World!");`


So this post is not quite a question, just a thought in my mind 
after I am able to differentiate compile time codes from runtime 
codes.


When a variable is passed into a function, is its name kept somewhere for the function to acceess

2015-11-10 Thread DlangLearner via Digitalmars-d-learn
Here is what I want to know: when a function is called, does this 
function can recovery the information about which variables pass 
their values into this function's arguments. I use the following 
example to show what I want to know.


void main(){
int a = 1;
writeln(fun(a.stringof, a));
}

//to return "a is assigned to 1"
string fun(string name, int x)
return name~" is assigned to "~x;
}

For this example my question turns to become: can the name for 
fun be derived instead of passing to it?


string fun(int x)
	//is there any way we can know that this value x is passed from 
the variable a in the main function?
	string name = the name of the variable which passes its value to 
x	

return name~" is assigned to "~x;
}

Please enlighten me if this can be done, thanks.


Re: When a variable is passed into a function, is its name kept somewhere for the function to acceess

2015-11-10 Thread DLangLearner via Digitalmars-d-learn
On Tuesday, 10 November 2015 at 14:22:49 UTC, Gary Willoughby 
wrote:
On Tuesday, 10 November 2015 at 14:14:33 UTC, DlangLearner 
wrote:

Please enlighten me if this can be done, thanks.


If i understand you, you could use a templated function:

import std.stdio;

void foo(alias a)()
{
writefln("%s was passed in.", a.stringof);
}

void main(string[] args)
{
auto bar = "bar";
foo!(bar);
}


This is what I want. Thank you so much, this is a really elegant 
solution.